Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

"Dash" Script

Discussion in 'Scripting' started by Garcia-Rojas, Aug 23, 2016.

  1. Garcia-Rojas

    Garcia-Rojas

    Joined:
    Sep 1, 2015
    Posts:
    16
    Hi! Im just having some problems with a "dash" script im writing. Right know I'm not able to make it work, as far as I have been able to doit is teleporting the player using transform.position = dashPoint but obviusly is not a dash :/. I need it to make it with a certain speed.

    Any help would be awesome adn thank you in advance :D

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. //<>
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.     public float speed = 6f;
    7.     public float Range =100f;
    8.     public float dashSpeed = 1f;
    9.  
    10.     bool dashing = false;
    11.  
    12.  
    13.  
    14.     Ray dashgRay;
    15.     RaycastHit dashHit;
    16.  
    17.  
    18.     Vector3 movement;
    19.     Vector3 clickPoint;
    20.     Vector3 dashPoint;
    21.  
    22.  
    23.     Rigidbody playerRigidbody;
    24.     int floorMask;
    25.     int dashableMask;
    26.     float camRayLength = 100f;
    27.  
    28.     void Awake()
    29.     {
    30.         floorMask = LayerMask.GetMask ("Floor");
    31.         dashableMask = LayerMask.GetMask ("Dashable");
    32.         playerRigidbody = GetComponent<Rigidbody> ();
    33.     }
    34.  
    35.     void FixedUpdate()
    36.     {
    37.         float h = Input.GetAxisRaw ("Horizontal");
    38.         float v = Input.GetAxisRaw ("Vertical");
    39.         ClickPointer ();
    40.         Dash ();
    41.         Move (h, v);
    42.         Turning ();
    43.  
    44.     }
    45.     void Move(float h, float v)
    46.     {
    47.         movement.Set (h, 0f, v);
    48.  
    49.         movement = movement.normalized * speed * Time.deltaTime;
    50.  
    51.         playerRigidbody.MovePosition (transform.position + movement);
    52.     }
    53.  
    54.     void Turning()
    55.     {
    56.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    57.  
    58.         RaycastHit floorHit;
    59.  
    60.         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
    61.         {
    62.             Vector3 playerToMouse = floorHit.point - transform.position;
    63.             playerToMouse.y = 0f;
    64.  
    65.             Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
    66.             playerRigidbody.MoveRotation (newRotation);
    67.  
    68.         }
    69.     }
    70.             //Checks the direcction where to dash
    71.     void ClickPointer()
    72.     {
    73.         if(Input.GetButtonDown ("Fire1")){
    74.        
    75.             Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    76.  
    77.             RaycastHit floorHit;
    78.  
    79.             if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) {
    80.                 clickPoint = floorHit.point;
    81. dashing = true;
    82.             }
    83.         }
    84.     }
    85.  
    86.     void Dash()
    87.     {
    88.                  //Finds the exact point where dash stops
    89.         if (Physics.Linecast(clickPoint, transform.position,out dashHit,dashableMask) && dashing == true )
    90.             {
    91.                 // Dash to that point
    92.             dashPoint = dashHit.point;
    93.  
    94.             playerRigidbody.MovePosition (transform.position + dashPoint.normalized * dashSpeed * Time.deltaTime);
    95.  
    96.             }
    97.     }
    98.  
    99.     }
    100.  
     
    Last edited: Aug 23, 2016
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    You'll either want to Lerp position, or use MoveTowards for change over time. You could do it in a dash coroutine perhaps, making sure to not allow other movement while the dash is in progress. I'm assuming the dash will last less than like 10 frames anyway, so it won't be a long process. Alternatively you could have the player teleport and leave a trail behind them, simulating movement.
     
  3. Garcia-Rojas

    Garcia-Rojas

    Joined:
    Sep 1, 2015
    Posts:
    16
    Thank you so much for the answer :D I did it with lerp :)
     
  4. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    Code (CSharp):
    1. how can I import dash movement to mobile touch. My game nearly finised, ı just need final mechanic. This code is not for mobile what I have to change ?
    2.  
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8. Vector3 moveDir;
    9. Rigidbody rb;
    10. float speed = 5f;
    11. float dashCooler = 0.5f;
    12. KeyCode dashKey;
    13. bool dashing = false;
    14. Vector3 dashDir;
    15. float dashTimer = 0f;
    16. void Start()
    17. {
    18. rb = GetComponent<Rigidbody>();
    19. }
    20. void Update()
    21. {
    22. moveDir = Vector3.zero;
    23. if (Input.GetKey(KeyCode.W))
    24. {
    25. moveDir = Vector3.forward;
    26. }
    27. else if (Input.GetKey(KeyCode.S))
    28. {
    29. moveDir = -Vector3.forward;
    30. }
    31. else if (Input.GetKey(KeyCode.A))
    32. {
    33. moveDir = -Vector3.right;
    34. }
    35. else if (Input.GetKey(KeyCode.D))
    36. {
    37. moveDir = Vector3.right;
    38. }
    39. if (Input.GetKeyDown(KeyCode.W))
    40. {
    41. CheckDash(KeyCode.W, Vector3.forward);
    42. }
    43. else if (Input.GetKeyDown(KeyCode.S))
    44. {
    45. CheckDash(KeyCode.S, -Vector3.forward);
    46. }
    47. else if (Input.GetKeyDown(KeyCode.A))
    48. {
    49. CheckDash(KeyCode.A, -Vector3.right);
    50. }
    51. else if (Input.GetKeyDown(KeyCode.D))
    52. {
    53. CheckDash(KeyCode.D, Vector3.right);
    54. }
    55. dashCooler -= Time.deltaTime;
    56. }
    57. void CheckDash(KeyCode key, Vector3 dir)
    58. {
    59. if (dashCooler > 0f && dashKey == key)
    60. {
    61. dashing = true;
    62. dashDir = dir;
    63. dashTimer = 0.5f;
    64. }
    65. else
    66. {
    67. dashKey = key;
    68. dashCooler = 0.5f;
    69. }
    70. }
    71. void FixedUpdate()
    72. {
    73. if (dashing)
    74. {
    75. rb.MovePosition(rb.position + dashDir * speed * 2 * Time.deltaTime);
    76. dashTimer -= Time.deltaTime;
    77. if (dashTimer <= 0f)
    78. {
    79. dashing = false;
    80. }
    81. }
    82. if (moveDir != Vector3.zero)
    83. {
    84. rb.MovePosition(rb.position + moveDir * speed * Time.deltaTime);
    85. }
    86. }
    87. }
    88.  
     
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    AnimationCurve will help you make your dash look as smooth as you want, just use it instead of lerp factor

    Code (CSharp):
    1. public AnimationCurve dashCurve;
    2. public float dashDuration = 0.5f; // seconds
    3.  
    4. private void Dash(){
    5.     transform.position = Vector3.Lerp(transformPosition, dashTargetPosition, dashCurve.Evaluate((Time.time - dashStartTime) / dashDuration);
    6. }