- Home /
Align rotation to a vector3 point
Hi, I'm trying to allign my "spawn projectile point" to where my mouse cursor is, but I'm having a bit of a problem. As you can see in the image, it has a slight offset.

I've set my player right hand IK to 1 already and I'm setting it to aim towards that silver sphere (that is my cursor position)
private void OnAnimatorIK()
{
// Weapon aim at target IK
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1f);
animator.SetIKPosition(AvatarIKGoal.RightHand, aimTarget.position);
}
The thing is that as you can see in the picture, the z-axis of my spawn point is a bit off. I have the code in the player to fire the bullet and pass the bulletSpawnPoint (that is a transform I have parented to the player bones to indicate where I want the bullet to spawn from) as an Euler Angle:
private void Fire()
{
var go = Instantiate(bulletPrefab);
go.transform.position = bulletSpawnPoint.position;
var bullet = go.GetComponent<Bullet>();
bullet.Fire(go.transform.position, bulletSpawnPoint.eulerAngles + aimTargetOffset, gameObject.layer);
}
And lastly I have a script in the bullet that tells it to move forward based on the parameters I've set
public void Fire(Vector3 position, Vector3 euler, int layer)
{
transform.position = position;
transform.eulerAngles = euler;
transform.position = new Vector3(transform.position.x, transform.position.y, 0f);
transform.rotation = Quaternion.LookRotation(transform.forward, Vector3.forward);
}
void Update()
{
transform.Translate(Vector3.forward * velocity * Time.deltaTime);
}
Does anyone have any ideas?
Thank you
Answer by whogas · Sep 02, 2020 at 03:32 PM
Not entirely sure about your rig setup here, but is it just that the right hand is pointing correctly, but that doesn't translate to the gun tip as you expect?

Based on your follow-up:
"The IK is ai$$anonymous$$g correctly since the joint is ai$$anonymous$$g at the sphere. The thing is though, that I would like the projectile that comes out ot the bulletSpawnPoint to be facing the sphere as well, because as you can see it will miss it by a little bit."
I understand what you're asking now. You should just be able to offset the ai$$anonymous$$g point by the hand-coordinate system's delta-Y to the height of the gun. So the hand is pointing at a location delta-Y down from the click point (in the hand's coordinate system).
Should be able to look at the coordinate systems for the hand, gun, and click point to figure this out.
Thanks for the reply. Any practical example that can help me out?
Answer by henmachuca · Sep 02, 2020 at 05:06 PM
The IK is aiming correctly since the joint is aiming at the sphere. The thing is though, that I would like the projectile that comes out ot the bulletSpawnPoint to be facing the sphere as well, because as you can see it will miss it by a little bit.
Your answer