Reputation: 9
The problem is on line 26. Just cant get it right. Help me please. I am creating a first person game and cant pass this ground check Issue.
velocity = -2f
public class movement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public Transform groundCheak;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheak.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Upvotes: 0
Views: 3636
Reputation: 3949
You can't assign float
directly to a Vector3
. As the name suggests Vector3
has 3 element. So you can do:
velocity = new Vector3(-2f, 0, 0);
Or
velocity = new Vector3(-2f, -2f, -2f);
Which has different meaning.
You can check out this or this which might be what you actually want to do.
Upvotes: 3