Topic was automatically imported from the old Question2Answer platform.
Asked By
rico345100
I want to create objects in front of player with some distance, so that prevents hit with player immediately. So first, I get the global player position and direction vector which player towards:
var player_position = player.transform.origin
var player_forward_vector = player.global_transform.basis.z
And then give additional distance:
var spawn_position = player_position
spawn_position.z -= player_forward_vector.z + 100
And then set the position:
var asteroid_instance = asteroid_scene.instance()
asteroid_instance.transform.origin = spawn_position
When I run the game, first it seems work, however when I rotated the player, still object spawned same position, not in front of where I’m looking at.
Here’s the video to demonstrate:
Why object doesn’t created in front of player, instead keep spawns in same location? Any advice will very appreciate it.
This line is the saviour. quick explanation: you want a vector 3 since translation of the obstacle is a vector 3. we get players rotation since he is rotating in a full circle but at first I tried aligning his angle with obstacle and adding some random vectors for each if statement BUT then I discovered the sinus and cosinus functions. Basically google trigonometric circle. sinus and cosinus is there to convert angles to a range from 0 to 1 blah blah so they are used to give us translation data from an angle. then I had slight issues that degrees suck at since I didnt know radians are what is the true power of game engines. by saying deg2rad() you are converting a value from degrees to radians and by doing that you get perfect values. and finally the - sign is what worked in my case since I believe I rotated my world wrong idk
If it doesnt work right, switch - with + or sinus with cosinus and I can guarantee you that it will work.