I have a script attached to the parent (an empty object) that should make one of the childs (a sphere) to move. But the child object does not actually move… it starts jittering as if trying to move but seems stuck in its original position. I used debug to see the starting and target positions and they work as they should. I tried changing position to localPosition in every place, but seems not to work either. Can child objects actually be moved?
[SerializeField] Vector3 posPlayer;
[SerializeField] Vector3 posNew;
private void Start()
{
posPlayer = this.transform.GetChild(1).transform.position;
posNew = new Vector3(10, 0, 10);
}
private void Update()
{
this.transform.GetChild(1).transform.position = Vector3.MoveTowards(posPlayer, posNew, Time.deltaTime * 1);
}
Welcome!
Is there any other code somewhere that might be trying to update the position of the element at the same time? Jittering in place is a good indicator of a conflict with one bit trying to move and object and another trying to keep it in place.
Thanks for the welcome! (Obviously that was my first post)
Yes, there seemed to be a conflict. Inside the MoveTowards, I changed posPlayer for the actual Game Object name and “transform.position” and it actually worked. So it looks like as below now – not sure why the first code was an issue.
Now:
this.transform.GetChild(1).transform.position = Vector3.MoveTowards(this.transform.GetChild(1).transform.position, posNew, Time.deltaTime * 1);
Before:
this.transform.GetChild(1).transform.position = Vector3.MoveTowards(posPlayer, posNew, Time.deltaTime * 1);
Edit: The code in the initial post was all the code existent in the script and whole project, so no other conflict could come from elsewhere.
Well that makes sense. In the first version, you were constantly setting the player to a position shifted just slightly away from the original player position (which you captured in the Start method). In the second version, you’re moving it to a position slightly away from its current position. Sounds like the latter is what you want.
1 Like