Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
58
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 mouseaim : MonoBehaviour
  6. {
  7. private Camera mainCamera;
  8. private Rigidbody rb;
  9.  
  10. public float moveSpeed;
  11. private Vector3 moveInput;
  12. private Vector3 moveVelocity;
  13. public float dashcd = 2f;
  14. public float dashcooldown;
  15. public bool dashavailable;
  16.  
  17. public float dashForce;
  18. public float dashDuration;
  19.  
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. mainCamera = FindObjectOfType<Camera>();
  24. rb = GetComponent<Rigidbody>();
  25. dashcooldown = dashcd;
  26. dashForce = 200;
  27. dashDuration = 0.5f;
  28. }
  29.  
  30. // Update is called once per frame
  31. void Update()
  32. {
  33.  
  34. // Dash Logic
  35. if (dashcooldown < dashcd)
  36. {
  37. dashcooldown = dashcooldown + 0.01f;
  38. }
  39. else
  40. {
  41. dashavailable = true;
  42. }
  43.  
  44.  
  45.  
  46. moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
  47. moveVelocity = moveInput * moveSpeed;
  48.  
  49. Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
  50. Plane groundPlane = new Plane (Vector3.up, Vector3.zero);
  51. float rayLength;
  52. if(groundPlane.Raycast(cameraRay, out rayLength))
  53. {
  54. Vector3 pointToLook = cameraRay.GetPoint(rayLength);
  55. transform.LookAt(pointToLook);
  56. if (Input.GetMouseButton(0))
  57. {
  58. if (dashavailable)
  59. {
  60. dashcooldown = 0f;
  61. dashavailable = false;
  62. StartCoroutine(Dash());
  63. }
  64. }
  65. }
  66. }
  67.  
  68. void FixedUpdate (){
  69. rb.velocity = moveVelocity;
  70. }
  71.  
  72. public IEnumerator Dash()
  73. {
  74. rb.AddForce(transform.forward * dashForce, ForceMode.VelocityChange);
  75. yield return new WaitForSeconds(dashDuration);
  76.  
  77. rb.velocity = moveVelocity;
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement