- Home /
Tail follow object - getting the point behind an object
Hi everyone, I have a problem with making body parts of a "snake" follow the head object.
A picture is worth a thousand words, so here's one:
Here's the code I have:
public void moveHead(){
mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint (mousePosition);
transform.position = Vector2.Lerp (transform.position, mousePosition, moveSpeed);
directionTransform = Quaternion.LookRotation(transform.position - mousePosition,Vector3.forward);
directionTransform.x = 0.0f;
directionTransform.y = 0.0f;
transform.rotation = Quaternion.Slerp (transform.rotation, directionTransform, Time.deltaTime * 8);
}
public void moveBodyParts(){
for (int i = 1; i < SnakeBody.Count; i++) {
**SnakeBody[i].transform.position = Vector2.Lerp (SnakeBody[i].transform.position,
(SnakeBody[i-1].transform.position-transform.forward*2)
, moveSpeed);**
}
and a thread I have used to come up with the erroneous line: http://answers.unity3d.com/questions/722047/get-the-position-behind-an-object-locally-.html
I am pretty sure this is where I mess things up:
SnakeBody[i].transform.position = Vector2.Lerp (SnakeBody[i].transform.position,
(SnakeBody[i-1].transform.position-transform.forward*2)
, moveSpeed);
What I aim to do is to move each "circle" to the (previous one's position-it's size*2) where 2 is some arbitrary number that should be the final distance between the two objects.
Would love some pointers on this :) Cheers!
also tried this, results in the same effect:
SnakeBody[i].transform.position = Vector2.Lerp (SnakeBody[i].transform.position,
(SnakeBody[i-1].transform.position-SnakeBody[i-1].transform.forward)
, moveSpeed);
SOLVED:
The issue was that I was using Vector3.forward on a 2D vector :)
This fixes it:
for (int i = 1; i < SnakeBody.Count; i++) {
SnakeBody[i].transform.rotation = Quaternion.Slerp (SnakeBody[i].transform.rotation, directionTransform, Time.deltaTime * 8);
SnakeBody[i].transform.position = Vector2.Lerp (SnakeBody[i].transform.position,
(SnakeBody[i-1].transform.position-(SnakeBody[i-1].transform.up))
, moveSpeed);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613194523im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Follow target script is rotating only clockwise. Something simple? 2 Answers
How to don't face player to camera direction? 1 Answer
Distance between 2 objects without their Z axis. 1 Answer
Allow to move object by the mouse position without missing any collisions 1 Answer
Lerp in direction to certain distance 3 Answers