Making a gameobject follow a player closely
So i'm trying to make a snake clone for practice and now i'm stuck at trying to make the tail or body follow the head. so this part here works fine
//reference to head
head = GameObject.FindGameObjectWithTag("Player").GetComponent();
//instantiate body below the head
body = Instantiate(bodySprite, new Vector2(head.position.x,head.position.y-0.5f), Quaternion.identity);
But when I add this part on the update method the body just go below the head so you can't see it
body.transform.position = Vector2.Lerp(transform.position,head.position,300*Time.deltaTime);
There's probably a better way of doing this and I would really appreciate it if you let me know.
Answer by streeetwalker · Apr 05, 2020 at 08:35 AM
Hi @shakeearth16, here's the problem: when you create the body, you offset it from the head. But when you LERP you don't use the same offset. The easiest way to fix your problem is to get the offset between head and body when you instantiate.
// instantiate first then:
Vector2 bodyOffset = body.transform.position - head.position ;
// then when you LERP, use this as the LERP TO point:
head.positon + bodyOffset;
Yeah I thought that was the case too but I didn't know how to. Thanks!
Your answer
Follow this Question
Related Questions
how to automatic add numbers in text after the slider finished? 0 Answers
Moving on the XZ plane 0 Answers
how to move a gameobject based on a dice roll 1 Answer
I need help with boss design. I am new to coding so any help would be nice. 0 Answers
New Vector2 won't take effect when called from another script in the same GameObject. 0 Answers