Making A 3D Maze Game in Unity - 2/10

Make a new c# script and name it as "myCharacterController". Add it with the player game object. Now open it and copy paste this code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class myCharacterController : MonoBehaviour {


Vector3 move;
CharacterController controller;

public float speed;
Vector3 startPosition=new Vector3(0,0,0);
// Use this for initialization
void Start () {
    move.y = 0f;
//  rb=GetComponent<Rigidbody>();
    controller = GetComponent<CharacterController> ();
}

// Update is called once per frame
void Update () {
    
    move.x=Input.GetAxis("Horizontal")*speed;
    move.y -= 9.8f * Time.deltaTime;
    move.z= Input.GetAxis("Vertical")*speed;
    if(!manager.isShowing){
    controller.Move (move*.5f);
    }
}

public void OnTriggerEnter (Collider col) 
{
    if (col.gameObject.CompareTag("Wall")){
        transform.position=Vector3.MoveTowards (transform.position,startPosition,1000000*Time.deltaTime);
    }

}

}

save it and go to unity. select the player game object. you will see "Speed" variable on the "myCharacterController" component from inspector. This value controls the speed of the player.

2.1.PNG

Create a point light as a child object of the player object. set it in the vector3(0,0,0) position. This is the setting I used for this point light.

2.2.PNG

I used cube to make some wall and this is my maze.

2.3.PNG

you may make your own. In the next section we will make some particle effects for the player.

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?