Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Animator))]
  6.  
  7. public class Player : MonoBehaviour
  8. {
  9. Animator anim;
  10.  
  11. private Rigidbody rb;
  12.  
  13. public int speed;
  14.  
  15. bool isMovementCalled = false;
  16. public float decelleration = 1.5f;
  17.  
  18.  
  19. // Start is called before the first frame update
  20. void Start()
  21. {
  22. anim = GetComponent<Animator>();
  23.  
  24. rb = GetComponent <Rigidbody>();
  25. }
  26.  
  27. private void FixedUpdate()
  28. {
  29. Movement();
  30. }
  31.  
  32.  
  33. private void Movement()
  34. {
  35. //calling our floats
  36. float moveHorizontal = Input.GetAxis("Horizontal");
  37. float moveVertical = Input.GetAxis("Vertical");
  38.  
  39.  
  40.  
  41.  
  42.  
  43. //tell us which way we're plannig to move
  44. Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
  45.  
  46.  
  47. //apply force to start moving
  48. rb.AddForce(movement * speed);
  49. rb.AddRelativeForce(-rb.velocity * decelleration);
  50.  
  51. if (rb.velocity.magnitude >= 0)
  52. {
  53. // Player isn't moving
  54. isMovementCalled = false;
  55. }
  56. }
  57.  
  58.  
  59.  
  60. // Update is called once per frame
  61. void Update()
  62. {
  63.  
  64.  
  65. if (Input.GetKeyDown(KeyCode.W) == true || Input.GetKeyDown(KeyCode.S) == true || Input.GetKeyDown(KeyCode.A) == true || Input.GetKeyDown(KeyCode.D) == true)
  66. {
  67. isMovementCalled = true;
  68. }
  69.  
  70.  
  71.  
  72.  
  73. if (isMovementCalled == true)
  74. {
  75. anim.SetBool("Walking", true);
  76. anim.SetBool("Idle", false);
  77. }
  78.  
  79. if (isMovementCalled == false)
  80. {
  81. anim.SetBool("Walking", false);
  82. anim.SetBool("Idle", true);
  83. }
  84.  
  85.  
  86.  
  87. }
  88.  
  89.  
  90. void OnAnimatorMove()
  91. {
  92. Animator animator = GetComponent<Animator>();
  93.  
  94. if (animator)
  95. {
  96. Vector3 newPosition = transform.position;
  97. newPosition.z += animator.GetFloat("Runspeed") * Time.deltaTime;
  98. transform.position = newPosition;
  99. }
  100. }
  101.  
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement