- Home /
Question by
yourchild321 · Dec 28, 2020 at 05:21 PM ·
move object
i don't know why controller.isGrounded is not working and the player change y position when i move some one can help me i m 14 and im new in C# and Unity
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovements : MonoBehaviour { CharacterController controller; public float speed = 12f; public float gravity = 20f; public float jump = 10f; Vector3 moveDir = Vector3.zero; void Start() { controller = GetComponent(); }
void Update()
{
if (controller.isGrounded)
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
moveDir = transform.right * horizontal + transform.forward * vertical;
if (Input.GetButton("Jump"))
{
moveDir.y = jump;
}
}
moveDir.y -= gravity;
controller.Move(moveDir * speed * Time.deltaTime);
}
}
Comment
Answer by wannaMakeAGame · Dec 28, 2020 at 05:37 PM
Probably you need a isGrounded bool variable at the top. And you need to make it false while not jumping.
Your answer
![](https://koobas.hobune.stream/wayback/20220613022310im_/https://answers.unity.com/themes/thub/images/avi.jpg)