- Home /
Question by
Bonnie49er1 · Jul 31, 2021 at 08:41 PM ·
errorraycast
Look Rotation viewing vector is zero. Raycast problem
Hello I am having an issue with my unity project. The problem is every time I "shoot" a probuilder cube it says "Look Rotation viewing vector is zero" it brings me to the Shoot() function I made
private void Shoot()
{
bang.Play();
readyToShoot = false;
//Spread
float x = Random.Range(-spread, spread);
float y = Random.Range(-spread, spread);
//Calculate Direction with Spread
Vector3 direction = FPSCam.transform.forward + new Vector3(x, y, 0);
//RayCast
if (Physics.Raycast(FPSCam.transform.position, FPSCam.transform.forward, out rayHit, range, whatIsEnemy))
{
Debug.Log(rayHit.collider.name);
if (rayHit.collider.CompareTag("Enemy"))
{
rayHit.collider.GetComponent<Enemy>().TakeDamage(damage);
}
}
Instantiate(bulletHoleGraphic, rayHit.point, Quaternion.LookRotation(rayHit.normal));
bulletsLeft--;
bulletsShot--;
Invoke("ResetShot", timeBetweenShooting);
if (bulletsShot > 0 && bulletsLeft > 0)
Invoke("Shoot", timeBetweenShots);
}
Comment
you use rayHit in the Instantiate method place and align the rotation but you don't make sure the Raycast actually hits something when Instantiating. Move the Instantiate inside the if(Raycast...{ } clause and it should be ok.
Your answer
Follow this Question
Related Questions
Raycast Shoot Error 1 Answer
(2D) Raycast check - null reference even when (RayHit != null) 2 Answers
LookRotation and Raycasting 2 Answers
Error in raycast using with drag drop of objects 0 Answers
Raycast hits and affects all items within the same tag 2 Answers