- Home /
Instantiate Object in direction from another
Hi,
I have a ship which is looking at a sphere. And I want to instantiate another object 90 degrees from the direction of the ship but 5f away from the center of the the sphere. It's difficult to understand but the image should help you to understand my problem.
Thanks for any help.
Answer by Baste · Oct 25, 2015 at 11:37 AM
If it's always exactly 90 degrees, it's real easy. You can use the ship's transform's "right" property, which is a vector that points to the transform's right, and is 1 long. So your spawn position is:
sphere.transform.position + (ship.transform.right * 5f)
If you need some arbitrary amount of degrees, it's a bit harded, but you can do some quaternion magic. If you multply a quaternion and a vector, the result is the vector rotated by the quaternion. So you have thing like this:
Quaternion.Euler(0, 90, 0) * Vector3 (0, 0, 1) == Vector3 (1, 0, 0)
So if you want the angle to be 45 instead, that would the the ships' forward rotated 180 - 45 = 135 degrees to the right on the y-axis (assuming you're seeing this from above), which would be:
Quaternion.Euler(0, 135f, 0) * ship.transform.forward;
Good luck!
Your answer
Follow this Question
Related Questions
Converting direction to Vector2 2 Answers
Angle between two objects' facing direction 2 Answers
how to drop mesh in all of directions? 0 Answers
turn 180 spaceship 0 Answers