- Home /
Object follow behind other object, wont stop spinning
I want to keep the yellow square in the same position relative to the purple box. So in this case to keep behind it, and it works. But the yellow square wont stop spinning around it's own Y axis
transform.position = (player.transform.position + Vector3(0,0,-1));
transform.RotateAround (player.transform.position, Vector3.up, player.transform.eulerAngles.y);
Answer by robertbu · Jul 28, 2013 at 04:31 PM
The easiest solution is to make the yellow box a child of the purple box. You can do it in the editor by dragging the yellow box on top of the purple box in the Hierarchy. You can also do it in code:
transform.parent = player.transform;
As for doing it separately in code, it will depend on the relationship between the orientation of the two. It appears that they both have the same rotation when following, so you might be able to just do:
transform.rotation = player.transform.rotation;
If neither of these two solutions is a good fit, let me know. There are other more complex methods of solving this problem.
I'm going to need that more complex way. If I try to get the yellow box to go off and pick up things, the purple box's movement messes it up.
You are going to need to explain your problem better. What axis of the purple box is the yellow box following? What do you mean "go off and pick up things and how is it to aligning while it is picking up things?"
If the yellow box is going to be doing some independent movement but you still want the yellow box pointed at the end of the purple box, you can put an empty game object at the end of the purple box and make it a child of the purple box. Then have the yellow box look at it each frame:
transform.lookAt(ObjectToLoookAtEachFRame.transform);
Note some aspects of rotation are better handled if you have the front of your object be the side that faces positive 'z' when there is no rotation.
I'm trying to make sparks from Spyro the Dragon It follows the purple box around, but when the purple box gets close to gems, the yellow box goes off and picks them up for you.
When the purple box is not near a gem, the yellow box needs to move like it was a child to the purple box
Your answer
Follow this Question
Related Questions
Rotate vector around vector? 2 Answers
Transform Rotate a certain angle (and back) 2 Answers
Rotate Object to Euler Angle using Physics? 3 Answers