Untitled
unknown
plain_text
4 years ago
931 B
12
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float jumpSpeed = 2;
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 * Time.deltaTime;
}
charCont.Move(moveDir * Time.deltaTime);
}
}
Editor is loading...