Advertisement
Guest User

Character movement working but dash is teleporting??

a guest
Apr 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. using UnityEngine; using System.Collections;
  2.  
  3. public class TileMovement : MonoBehaviour {
  4.  
  5. public string LastMoveDirection;
  6. private float speed = 2.0f;
  7. public float dash_speed = 0f;
  8. public string RotateCharacter;
  9. private bool Rotated = false;
  10. [SerializeField] private Vector3 pos;
  11. private Transform transform;
  12. private CharacterController characterController;
  13. private Vector3 lastRoundedPosition;
  14. void Awake()
  15. {
  16. //Here we set the Values from the current position
  17. pos = base.transform.position;
  18. transform = base.transform;
  19. characterController = this.GetComponent<CharacterController>();
  20. }
  21. //void OnControllerColliderHit(ControllerColliderHit hit)
  22. //{
  23. // Debug.Log("Hit somthing " + hit.transform.tag);
  24. // if (hit.transform.tag == "FloorTile")
  25. // {
  26. // hit.gameObject.GetComponent<TileRotation>().FlipTile();
  27. // }
  28. //}
  29. void Update()
  30. {
  31. Movement();
  32. // transform.position = pos;
  33. }
  34. private void Movement()
  35. {
  36. //If we press any Key we will add a direction to the position ...
  37. //using Vector3.'anydirection' will add 1 to that direction
  38. //But we Check if we are at the new Position, before we can add some more
  39. //it will prevent to move before you are at your next 'tile'
  40. if (Input.GetButtonDown("Horizontal"))
  41. {
  42. if (Input.GetAxisRaw("Horizontal") == 1) //&& rigidbody.position == pos)
  43. {
  44. // pos += Vector3.right;
  45. transform.rotation = Quaternion.LookRotation(Vector3.right);
  46. if(Input.GetButton("LeftShift"))
  47. {
  48. characterController.Move(Vector3.right*dash_speed);
  49. }
  50. else
  51. {
  52. characterController.Move(Vector3.right);
  53. }
  54. LastMoveDirection = "right";
  55. }
  56. else if (Input.GetAxisRaw("Horizontal") == -1)// && rigidbody.position == pos)
  57. {
  58. // pos += Vector3.left;
  59. transform.rotation = Quaternion.LookRotation(Vector3.left);
  60. if (Input.GetButton("LeftShift"))
  61. {
  62. characterController.Move(Vector3.left*dash_speed);
  63. }
  64. else
  65. {
  66. characterController.Move(Vector3.left);
  67. }
  68. LastMoveDirection = "left";
  69. }
  70. }
  71. if (Input.GetButtonDown("Vertical"))
  72. {
  73. if (Input.GetAxisRaw("Vertical") == 1)//&& rigidbody.position == pos)
  74. {
  75. // pos += Vector3.forward;
  76. transform.rotation = Quaternion.LookRotation(Vector3.forward);
  77. if (Input.GetButton("LeftShift"))
  78. {
  79. characterController.Move(Vector3.forward*dash_speed);
  80. }
  81. else
  82. {
  83. characterController.Move(Vector3.forward);
  84. }
  85. LastMoveDirection = "forward";
  86. }
  87. else if (Input.GetAxisRaw("Vertical") == -1)// && rigidbody.position == pos)
  88. {
  89. // pos += Vector3.back;
  90. transform.rotation = Quaternion.LookRotation(Vector3.back);
  91. if (Input.GetButton("LeftShift"))
  92. {
  93. characterController.Move(Vector3.back*dash_speed);
  94. }
  95. else
  96. {
  97. characterController.Move(Vector3.back);
  98. }
  99. LastMoveDirection = "back";
  100. }
  101. }
  102. // characterController.Move(new Vector3(Mathf.FloorToInt(characterController.transform.position.x), 0, Mathf.FloorToInt(characterController.transform.position.z)));
  103. characterController.transform.position = new Vector3(
  104. Mathf.RoundToInt(characterController.transform.position.x),
  105. Mathf.RoundToInt(1),
  106. Mathf.RoundToInt(characterController.transform.position.z)
  107. );
  108. }
  109. //Here you will move Towards the new position ...
  110. //transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement