Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.SceneManagement;
- using UnityEngine;
- public class Controls : MonoBehaviour {
- public float speed;
- private Rigidbody rb;
- Vector3 movement;
- public float moveHorizontal;
- public float moveVertical;
- public Vector3 jump;
- public float jumpForce;
- public bool isGrounded = true;
- private float curLocX;
- private float prevLocX;
- private float curLocZ;
- private float prevLocZ;
- void Awake(){
- rb = GetComponent<Rigidbody> ();
- jump = new Vector3(0.0f, 2.0f, 0.0f);
- }
- void OnCollisionStay() {
- isGrounded = true;
- }
- void Move (float moveHorizontal, float moveVertical){
- InputListen ();
- movement.Set (moveHorizontal, 0f, moveVertical);
- movement = movement.normalized * speed * Time.deltaTime;
- rb.MovePosition (transform.position + movement);
- if (transform.position.x != prevLocX && isGrounded || transform.position.z != prevLocZ && isGrounded) {
- transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement), 0.15F);
- }
- }
- void FixedUpdate () {
- float moveHorizontal = Input.GetAxisRaw ("Horizontal");
- float moveVertical = Input.GetAxisRaw ("Vertical");
- Move (moveHorizontal, moveVertical);
- if (Input.GetKey (KeyCode.Space) && isGrounded == true) {
- rb.AddForce(jump * jumpForce, ForceMode.Impulse);
- isGrounded = false;
- }
- }
- private void InputListen() {
- prevLocX = curLocX;
- curLocX = transform.position.x;
- prevLocZ = curLocZ;
- curLocZ = transform.position.z;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement