Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Third_Motor : MonoBehaviour {
  6.  
  7. public Vector3 CameraDirection;
  8.  
  9. private Vector3 MoveDirection = Vector3.zero;
  10.  
  11. public float WalkSpeed = 10f;
  12. public float RunSpeed = 20f;
  13.  
  14. public float jumpSpeed = 12;
  15. public bool grounded = false;
  16. private Rigidbody rBody;
  17.  
  18.  
  19.  
  20. void Awake()
  21. {
  22. rBody = GetComponent<Rigidbody>();
  23. }
  24.  
  25.  
  26.  
  27.  
  28. // Update is called once per frame
  29. void Update () {
  30.  
  31. // gets the input from the keys up.down.left.right and makes the new vector
  32. MoveDirection = new Vector3(Input.GetAxis("Horizontal"),0, Input.GetAxis("Vertical"));
  33.  
  34. ///Makes the charcater move in the foward direction
  35. MoveDirection = transform.TransformDirection(MoveDirection);
  36.  
  37. if(MoveDirection.magnitude > 1.0f || MoveDirection.magnitude < 0f)
  38. {
  39. MoveDirection.Normalize();
  40. }
  41.  
  42. float gravity = Physics.gravity.y * 10;
  43.  
  44. Vector3 move = rBody.velocity;
  45.  
  46. RaycastHit hit;
  47.  
  48. if(Physics.Raycast(transform.position, Vector3.down, out hit, 0.1f))
  49. {
  50. Debug.DrawLine(transform.position, hit.point);
  51. grounded = true;
  52. }
  53. else
  54. {
  55. grounded = false;
  56. }
  57.  
  58. if(grounded){
  59. if(Input.GetKeyDown(KeyCode.Space)){
  60. move.y = jumpSpeed;
  61. grounded = false;
  62. }
  63. move = MoveDirection * WalkSpeed;
  64. move.y = gravity * Time.deltaTime;
  65.  
  66. } else {
  67. move.x = Mathf.Clamp(move.x + MoveDirection.x * 0.01f * WalkSpeed, -WalkSpeed, WalkSpeed);
  68. move.y = Mathf.Clamp(move.y + gravity * Time.deltaTime, gravity, jumpSpeed);
  69. move.z = Mathf.Clamp(move.z + MoveDirection.z * 0.01f * WalkSpeed, -WalkSpeed, WalkSpeed);
  70. }
  71.  
  72. move = MoveDirection * WalkSpeed;
  73. move.y = gravity * Time.deltaTime;
  74.  
  75. transform.rotation = Third_Camera.Instance.transform.rotation;
  76. Vector3 euler = transform.eulerAngles;
  77. euler.x = 0;
  78. transform.eulerAngles = euler;
  79.  
  80.  
  81. rBody.velocity = move;
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement