Quaternion position changing.

I have this very simple 3D game where I have turrets shooting at incoming cube.

Aiming with quaternion and projectile instanting works but I need some help with finetuning.

  1. How to modify projectile spawn location? My projectile creation looks like this: Instantiate(bulletPrefab,transform.position, transform.rotation); //transform = my turret However the bullets come from the pivot point of my turret, when the actual gunpipe is slighlty above it. What kind of command could I use to for example add 2 to X position, where the bullets would spawn. Apparently positions and rotations are saved as these quaternions and I'm not familiar with handling them.

  2. What kind of command I could use to make simple preaiming for my turrets? I was thinking of getting somehow the enemys moving direction and then adding maybe empty game object towards it to use as place where to shoot.

3 Answers

3
  1. Found out solution for this one.

    Vector3 newPosition = new Vector3(transform.position.x, transform.position.y +1, transform.position.z); // transform = my turret

    And then spawning bullets with

    Instantiate(panosPrefab,newPosition, transform.rotation); // transform = my turret

For preaiming, you could get the enemy's transform, then add some vector to it (like, transform.forward * speed) -- requires tuning, then use Transform.TransformDirection on that position vector and use that to preaim.

Vector3 aimpos = enemyTransform.position + enemyTransform.TransformDirection(Vector3.forward * enemySpeed);

Thanks much Adam! I will try that when I get to it.