Hi, so I’m following this tutorial:
VIDEO
But I changed it so that the gun instantiates lasers instead of firing raycasts. That worked until I got to the bloom because Instantiating requires a Quaternion
whereas raycasting requires a Vector3
. I want to know how to put the Vector3
variable t_bloom
into the rotation slot of Instantiate.
Transform t_spawn = transform.Find("Cameras/Camera");
//Bloom
Vector3 t_bloom = t_spawn.position + t_spawn.forward * 1000f;
t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * t_spawn.up;
t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * t_spawn.right;
t_bloom -= t_spawn.position;
t_bloom.Normalize();
Debug.Log(t_bloom);
//Spawn Bullet
GameObject t_newBullet = Instantiate(bulletPrefab, t_spawn.position, Quaternion.Euler(t_bloom));
This is the section dealing with firing. I looked at other questions like this and found I could use Quaternion.Euler(Vector3)
to convert Vector3
s to Quaternion
s, but when I tried that, all the bullets always fired in the exact same direction no matter what direction I am facing.
Quaternion.LookRotation(dir,up)
turns a direction Vector3
s into a Quaternion
.
Quaternion.Euler( angles )
converts 3 angle values in degrees into a Quaternion
.
Try this:
// Calculate the direction vector for bullet firing
Vector3 t_bloom = t_spawn.position + t_spawn.forward * 1000f;
t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * t_spawn.up;
t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * t_spawn.right;
Vector3 bulletDirection = t_bloom.normalized;
// Create a rotation quaternion based on the direction vector
Quaternion bulletRotation = Quaternion.LookRotation(bulletDirection);
// Spawn Bullet
GameObject t_newBullet = Instantiate(bulletPrefab, t_spawn.position, bulletRotation);
Try this:
// Calculate the direction vector for bullet firing
Vector3 t_bloom = t_spawn.position + t_spawn.forward * 1000f;
t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * t_spawn.up;
t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * t_spawn.right;
Vector3 bulletDirection = t_bloom.normalized;
// Create a rotation quaternion based on the direction vector
Quaternion bulletRotation = Quaternion.LookRotation(bulletDirection);
// Spawn Bullet
GameObject t_newBullet = Instantiate(bulletPrefab, t_spawn.position, bulletRotation);