- Home /
Instantiate Projectile towards player (regardless of transform.rotation)
I have an enemy in my game who (as you'd expect) shoots at me if I'm in range. I have a gun attached to the enemy model, and a script which instantiates a projectile at the end of it(using a bulletSpawn empty GameObject). The problem is, as part of the shooting animation the gun isn't facing directly at me.
How can I instantiate the bullet in the direction of the player?
Thanks
Answer by ArkaneX · Jan 30, 2014 at 04:38 PM
var direction = enemy.transform.position - projectileStartPosition;
var projectile = (Transform)Instantiate(projectilePrefab, projectileStartPosition, Quaternion.LookRotation(direction));
Or, alternatively, you can call
projectile.LookAt(enemy.transform);
just after instantiating.
I've tried both of the above and I'm getting the same issue - which I have just realised I failed to mention initially - the 'bullet' appears to be firing past my(the player character) left shoulder. Is there something I need to change to have it direct to the center of my player transform?
it must be that the centre of your player is at the left shoulder.
now you could either find a way to move the centre of your character to the actual centre,
or you could do it the weird way and add a child empty gameobject of the character at the centre and aim at that,
or you could even set an offset for the position you are considering for the character and adjust it depending on the direction your character
i would try them in order... the last one seems quite last resort
I've tried everything mentioned here and still the shot is being fired in the wrong direction. Attached is a screen capture to show where the bullet is going compared to the center position.
Transform bullet = Instantiate(bulletPrefab, bulletSpawn.position, Quaternion.LookRotation((PlayerCenter.position - bulletSpawn.position))) as Transform;
The above is my instantiation, and PlayerCenter is found via:
Player = GameObject.FindGameObjectWithTag("Player");
PlayerCenter = Player.transform.FindChild ("Center");
I now have a blank GameObject positioned at the center of my player character.
Thanks
Could you compare bullet.forward to normalized vector of direction from spawn point to player center, and see if it shows true or false?
Debug.Log(bullet.forward == (PlayerCenter.position - bulletSpawn.position).normalized);
ArkaneX - the above debug shows true. I have noticed that while the enemy is shooting (in the state of shooting at me, not during the shot itself) if I move slightly to the left/right the bullet direction doesn't change, meaning if I move enough to the left I can have the bullet skim over my right shoulder and not collide (the above screenshot collides as expected). Thanks
Answer by supermap · Jan 30, 2014 at 04:45 PM
You substract the position of the bullet from the position of the player. That should give you a vector in the direction the bullet should take.
Normalize it then multiply it by the speed you want and you have your vector for the velocity of the bullet.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132112im_/https://answers.unity.com/themes/thub/images/avi.jpg)