- Home /
Target a point in space relative to gameObject
Hello
I am making a demo of a sci-fi game in space.
I have a missile, that flies to certain target. I also have a spaceship GameObject. Inside this spaceship GameObject there is a ShipBody with Box Collider, and this Collider is the target for the missile. When I fire the missile it aligns to the target and moves slowly to it - this works fine for me. But after launching multiple missiles at once I saw, that they all fly straight to the center of the ShipBody, and it looks unrealistic. So I made a function that when a missile is acquiring target it also generates random offset inside the mesh of the Collider, so missiles seem to hit different spots of the ShipBody - and it works fine.
The problem is when I rotate the ship. It still flies to the position+offset, but it do not recognize the rotation of the main object. I hope image will clarify this.
I dont want to do this by just creating empty GameObject inside SpaceShip - what if there will be hundred missiles and dozens of ships. I dont want to litter the scene with useless GameObjects. I will do this, if this is the only way, but If there is more elegant one, I will try to implement it.
Answer by toddisarockstar · Oct 14, 2018 at 05:18 AM
you are going to need to use local position. local positions are relative to a parent object's rotation and position. if you dont want to use empty objects as markers then you can use transform.InverseTransformPoint to store a local position relative to the ship.
you can then use transform.TransformPoint to get the point back with consideration of the ships position and rotation. transform.InverseTransformPoint
Can you tell me how my operations should commence?
I have:
GameObject ship
Vector3 hotspot - this is my offset from the ship center
GameObject cube - cube for reference inside the ship. Vector3 identical to cube.transform.position should be the outcome of the operation.
What should I do to calculate the outcome?
Answer by Lagger625 · Oct 14, 2018 at 05:51 AM
You can multiply the random offset by the spaceship transform.right, transform.up and transform.forward every frame to get an updated offset. Those values consider the gameObject rotation.
Please, can you tell me how to multiply this offset with those vectors?
Your answer
Follow this Question
Related Questions
My collider is offset from where it appears in the scene editor when I'm in gameplay mode 1 Answer
Camera dont move whit Smooth Follow 2D 0 Answers
Animation Stops Unexpected. (Script included) 0 Answers
How to make an enemy chase you and catch you when close (like slendytubbies) 0 Answers
How do I create AI with a good side and evil side? 0 Answers