Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.SceneManagement;
  4. using UnityEngine;
  5.  
  6. public class Controls : MonoBehaviour {
  7. public float speed;
  8. private Rigidbody rb;
  9. Vector3 movement;
  10. public float moveHorizontal;
  11. public float moveVertical;
  12.  
  13. public Vector3 jump;
  14. public float jumpForce;
  15.  
  16. public bool isGrounded = true;
  17.  
  18. private float curLocX;
  19. private float prevLocX;
  20. private float curLocZ;
  21. private float prevLocZ;
  22.  
  23. void Awake(){
  24. rb = GetComponent<Rigidbody> ();
  25. jump = new Vector3(0.0f, 2.0f, 0.0f);
  26. }
  27.  
  28. void OnCollisionStay() {
  29. isGrounded = true;
  30. }
  31.  
  32. void Move (float moveHorizontal, float moveVertical){
  33.  
  34. InputListen ();
  35.  
  36. movement.Set (moveHorizontal, 0f, moveVertical);
  37.  
  38. movement = movement.normalized * speed * Time.deltaTime;
  39.  
  40. rb.MovePosition (transform.position + movement);
  41.  
  42. if (transform.position.x != prevLocX && isGrounded || transform.position.z != prevLocZ && isGrounded) {
  43. transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement), 0.15F);
  44. }
  45.  
  46. }
  47.  
  48. void FixedUpdate () {
  49.  
  50. float moveHorizontal = Input.GetAxisRaw ("Horizontal");
  51. float moveVertical = Input.GetAxisRaw ("Vertical");
  52. Move (moveHorizontal, moveVertical);
  53.  
  54. if (Input.GetKey (KeyCode.Space) && isGrounded == true) {
  55. rb.AddForce(jump * jumpForce, ForceMode.Impulse);
  56. isGrounded = false;
  57. }
  58.  
  59.  
  60. }
  61.  
  62. private void InputListen() {
  63. prevLocX = curLocX;
  64. curLocX = transform.position.x;
  65.  
  66. prevLocZ = curLocZ;
  67. curLocZ = transform.position.z;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement