Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float moveSpeed;
public Rigidbody rb;
public float jumpForce = 5f;
public bool isOnGround = true;
Vector3 direction;
// To make camera move in mouse direction
public Vector2 turn;
// The sensetivity of mouse
public float sensitivity = 5f;
public Vector3 velocity;
// For dashing
public bool dashing = true;
public float dashingPower = 20f;
public float dashingTime = 0.3f;
public float dashingCooldown = 0.75f;
[SerializeField] private TrailRenderer tr;
// Start is called before the first frame update
void Start()
{
moveSpeed = 11f;
rb = GetComponent();
// To hide the mouse on game sreen
Cursor.lockState = CursorLockMode.Locked;
tr.emitting = false;
}
// Update is called once per frame
void Update()
{
// To move the camera in the direction of mouse
turn.x += Input.GetAxis("Mouse X") * sensitivity;
transform.localRotation = Quaternion.Euler(0, turn.x, 0);
transform.Translate(moveSpeed*Input.GetAxis("Horizontal")*Time.deltaTime,0f,moveSpeed*Input.GetAxis("Vertical")*Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isOnGround = false;
}
if (Input.GetKeyDown(KeyCode.Mouse0) && dashing)
{
StartCoroutine(Dash());
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isOnGround = true;
}
}
private IEnumerator Dash()
{
dashing = true;
tr.emitting = true;
velocity = new Vector3(transform.forward.x * dashingPower, 0f, transform.forward.z * dashingPower);
yield return new WaitForSeconds(dashingTime);
velocity = Vector3.zero;
tr.emitting = false;
yield return new WaitForSeconds(dashingCooldown);
dashing = true;
}
}