- Home /
Dropping an object behind player in respect to rotation,How to drop an object behind the player
Hello everyone
I'm making a 2D multiplayer racing game. And I want upon a keystroke to instantiate a object behind my current position.
I tried to use my current position while updating the y value to y - 1. It dosen't work on turns and many other scenarios due to rotation.
How can I instantiate an object behind my current position in respect to rotation? Thanks in advance!
I kinda solved it..
I created an empty gameObejct which serves as a tail for my player. This way the instantiate doesn't use the players transform but this tail's.
Is it perfect? probably not, is it working? yes! But I would still love to hear (read..) another solution.
Well you could always just use the players forward position and put it behind him... Like this.
GameObject go = Instantiate(prefab, player.transform.position + (player.transform.forward * -1), Quaternion.Identity);
Answer by Priyanka-Rajwanshi · Mar 24, 2018 at 07:17 AM
If you want to instantiate at the back with respect to object rotation, you should use transform.up or transform.right instead of subtracting values individually from y or x component.
Vector3 newPos = transform.position - transform.up;
Instantiate(objToInstantiate, newPos, Quaternion.identity);
Your answer
Follow this Question
Related Questions
transform position and rotation of instantiated object 1 Answer
How to instantiate object at particular location 1 Answer
"Object reference not set to an instance of an Object" 0 Answers
Instantiate Bullet Hole position and rotation, Vector3 issues 1 Answer
Object instantiate, Projectile problems 2 Answers