Untitled

 avatar
unknown
plain_text
3 years ago
914 B
2
Indexable
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);
    }

}
Editor is loading...