using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;
public Rigidbody rig;
private bool isGrounded;
// Update is called once per frame
void Update()
{
float x = Input.GetAxisRaw("Horizontal") * moveSpeed;
float z = Input.GetAxisRaw("Vertical") * moveSpeed;
rig.velocity = new Vector3(x, rig.velocity.y, z);
Vector3 vel = rig.velocity;
vel.y = 0;
if (vel.x != 0 || vel.z != 0)
{
transform.forward = rig.velocity;
}
if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
isGrounded = false;
rig.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
void OnCollisionEnter(Collision collision)
{
if(collision.GetContact(0).normal == Vector3.up)
{
isGrounded = true;
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter