C# SCRIPT
C# SCRIPT
public float walkSpeed = 6.0F;
public float jumpSpeed = 8.0F;
public float runSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= walkSpeed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
LAYERS
Layer is an important part of unity. When you try to make a games , you need a lot of objects and you need to make sure which layer you actually want to use. Unity has a options for those layers known as sorting layer.
For Example:
You make a games and in the inspector section you set player and ground both default or same layer and add physics . When you start the game and notice that your player falling or moving through the game objects like tree or walls . this the reasons you set the in the same layer.
How To Solve?
Just click on a game objects and in the inspector section you can see a layer option in the top corner , click and find a option names add sorting layer and here you can add a lot of layers you want. Create layers and set the player and the games objects in different layer.