Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class mouseaim : MonoBehaviour
- {
- private Camera mainCamera;
- private Rigidbody rb;
- public float moveSpeed;
- private Vector3 moveInput;
- private Vector3 moveVelocity;
- public float dashcd = 2f;
- public float dashcooldown;
- public bool dashavailable;
- public float dashForce;
- public float dashDuration;
- // Start is called before the first frame update
- void Start()
- {
- mainCamera = FindObjectOfType<Camera>();
- rb = GetComponent<Rigidbody>();
- dashcooldown = dashcd;
- dashForce = 200;
- dashDuration = 0.5f;
- }
- // Update is called once per frame
- void Update()
- {
- // Dash Logic
- if (dashcooldown < dashcd)
- {
- dashcooldown = dashcooldown + 0.01f;
- }
- else
- {
- dashavailable = true;
- }
- moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
- moveVelocity = moveInput * moveSpeed;
- Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
- Plane groundPlane = new Plane (Vector3.up, Vector3.zero);
- float rayLength;
- if(groundPlane.Raycast(cameraRay, out rayLength))
- {
- Vector3 pointToLook = cameraRay.GetPoint(rayLength);
- transform.LookAt(pointToLook);
- if (Input.GetMouseButton(0))
- {
- if (dashavailable)
- {
- dashcooldown = 0f;
- dashavailable = false;
- StartCoroutine(Dash());
- }
- }
- }
- }
- void FixedUpdate (){
- rb.velocity = moveVelocity;
- }
- public IEnumerator Dash()
- {
- rb.AddForce(transform.forward * dashForce, ForceMode.VelocityChange);
- yield return new WaitForSeconds(dashDuration);
- rb.velocity = moveVelocity;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement