Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
914 B
0
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float jumpSpeed = 3;
    public float gravity = -9.81f;
    public float speed = 2;
    public CharacterController charCont;
    public Vector3 moveDir;
    


    private void Awake()
    {
        charCont = gameObject.GetComponent<CharacterController>();
    }

    private void Update()
    {
        moveDir = new Vector3(Input.GetAxis("Horizontal") * speed, 0f, Input.GetAxis("Vertical") * speed);

        if (charCont.isGrounded)
        {
            moveDir.y = gravity;

            if (Input.GetKeyDown(KeyCode.Space))
            {
                moveDir.y = jumpSpeed;
            }
        }

        else
        {
            moveDir.y += gravity;
        }

        charCont.Move(moveDir * Time.deltaTime);
    }

}