Hi.
I want to move object from one point to another smoothly in two directions. Before i used time and it give me good movement from part 1 to 2 to 3 but I couldn’t invert it from part 3 to 2 to 1. Is there any idea how to do it ?
Here is my script:
public Transform target;
public float dis, zz;
// Update is called once per frame
void Update()
{
dis = transform.position.y - target.transform.position.y;
transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, zz);
// part 1
if (dis < 0.0f && dis > -0.3f)
{
zz = -0.3f;
}
// part 2
if (dis < 0.31f && dis > -0.6f)
{
zz = -0.6f;
}
// part 3
if (dis < 0.61f && dis > -0.9f)
{
zz = -0.9f;
}
Line 8 is using zz but its value is not being set until later down the code. Typically, you would want to calculate a value and then use it immediately. Otherwise, each frame you will be using the value from the previous frame.
Also, zz is being set an absolute value. You do not seem to be adding it to anything so I’m not sure how you are getting movement from that code. Maybe you are moving the parent object as well?
Typically, if you want to move something smoothly between points over time, you would consider using Vector3.Lerp. You would Lerp between some start point and your target end point. You would need to switch round your target end point each time the Lerp reaches 1 to get that ‘patrolling between 2 points’ effect.
It looks like you are trying to calculate the speed as a function of the distance from the target. In that case, play around with the Lerp parameters to get the effect you like. Maybe try Lerp(transform.position, targetPosition, 0.1f).
I tried Lerp before and failed because its a child object. I used time method. It’s strange method and difficult to read, but worked except one thing that need to fix. Look at this script:
when zz value is 0.5f and get back to 0.1f it moves not smoothly. especially part “forward 1”. I stopped here and I hope some one here tell me what to do.
Thank you DOUG_B for your points and details.
Will lerp do the same thing if i have 8 points ?
Also, If i make animation for child object, will that work the same if parent rotate and moves around ?
However, it wouldn’t be too difficult to make that change. In the code I gave, you would need to add more ‘waypoints’ (I would suggest an array or a List). Then when the object is within the turning point, rather than just swapping target between vPosA / vPosB, you would need to make the current position be first pos in Lerp and the next pos in the array/List being the next (taking care to handle the end of the list) - e.g.