I’m currently making a game and i was going to implement dashing in the game when i encountered a tiny problem. In the rb.AddForce(Camera.main.transform.forward * dashForce, ForceMode.Force); statement there was an error and i could fix it by removing Camera.main. Why is this happening, Below is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dash : MonoBehaviour
{
[SerializeField] private float dashForce; // should be 30000 or 35000 or somewhere in between or roughly around these numbers
[SerializeField] private float dashDuration;
public float dashCoolDownTime = 3;
public KeyCode dashKey;
private float nextDashTime;
private Rigidbody rb;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
if (Time.time > nextDashTime)
{
if (Input.GetKeyDown(dashKey))
{
StartCoroutine(Do_Dash());
}
}
}
public IEnumerator Do_Dash()
{
rb.AddForce(Camera.main.transform.forward * dashForce, ForceMode.Force);
yield return new WaitForSeconds(dashDuration);
rb.velocity = Vector3.zero;
nextDashTime = Time.time + dashCoolDownTime;
}
}
Btw, i am using the dash Tutorial from DapperDino (Creating Genji - Dash & Double Jump - Part 1 - Unity Tutorial - YouTube).