0

I have to move object from -10 to +10 x position. But I want to start my object from zero x.point to move, how can I start my lerp at the middle position?

Edit2: Object should start at x = 0 position and move to +10 x position and go to -10 x position and again +10 x, -10 x like a loop.

Vector3 pos1, pos2, pos0, pos3;
void Start()
{
    pos0 = transform.position;

    pos1 = pos0 + new Vector3(10, 0, 0);

    pos2 = pos0 - new Vector3(10, 0, 0);

    pos3 = pos1;
}
float time = 0.5f;
void FixedUpdate()
{ 
    time += Mathf.PingPong(Time.time * 0.5f, 1.0f);
    transform.position = Vector3.Lerp(pos2, pos1, time);
}
Tolga Yavuz
  • 159
  • 2
  • 10
  • make sure x is 0 at the beginning? Which it kinds is as your thing is from -10 to 10 from where it started.. You probably didnt want time.time but time.deltatime.. – BugFinder May 01 '19 at 13:19
  • transform.position.x is 0 at begining but when I start my Lerp ( it is from -10 to +10) transform.position.x becomes -10 and starts go to +10. – Tolga Yavuz May 01 '19 at 13:22
  • Then just Lerp from 0 to 10? Why are you trying to Lerp from -10 when the gameObject is at zero? Just modify your Lerp call to go from 0 to 10 and adjust the timing to get what you want. Trying to do it this way is just hacky. – TheBatman May 01 '19 at 13:26
  • Id say its either as Tolga has suggested or, your idea of time.time * 0.5 pingponged about,you're not getting the values you thought.. – BugFinder May 01 '19 at 13:30
  • @BugFinder I'm debugging for hours almost but couldn't find the right solution just needing a hand to solve problem I'm not taking advantage from others. – Tolga Yavuz May 01 '19 at 13:50

2 Answers2

3

From Unity API documentation at https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

public static Vector3 Lerp(Vector3 a, Vector3 b, float t);

When t = 0 returns a. When t = 1 returns b. When t = 0.5 returns the point midway between a and b.

In this symmetrical situation where x = 0 is exactly between start and end point, you could just use lerp with t starting at t = 0.5. Perhaps something like this:

Vector3 pos1, pos2, pos0, pos3;

private float t;

void Start()
{
    pos0 = transform.position;
    pos1 = pos0 + new Vector3(10, 0, 0);
    pos2 = pos0 - new Vector3(10, 0, 0);
    pos3 = pos1;

    t = 0.5
}

void FixedUpdate()
{ 
    t += Mathf.PingPong(Time.deltaTime * 0.5f, 1.0f);
    transform.position = Vector3.Lerp(pos2, pos1, t);
}

And as pointed out by @BugFinder, you should probably use Time.deltaTime instead of Time.time

Jizhou Yang
  • 138
  • 5
  • 1
    what was "t" for? – BugFinder May 01 '19 at 13:30
  • The time used for Lerp. Edited now. – Jizhou Yang May 01 '19 at 13:32
  • With your code it stucks at +10 x position at the begining. I need to start my gameobject starts at 0 position and move to +10 and move back to -10 and +10 again like a loop. Thank you for your answer anyway but it isn't the solution :/ – Tolga Yavuz May 01 '19 at 13:42
  • Well if he wants a PingPong type behavior he can still use `Time.time` that would just be `float t = (Mathf.sin(Time.time) + 1f) / 2;` the issue is the starting offset... – AresCaelum May 01 '19 at 13:42
  • @Eddge your code works fine but it is not linear, it slows down while closing to target position, is there any chance to make it linear without slowing? – Tolga Yavuz May 01 '19 at 13:45
1

Here is how I would approach it:

Vector3 ToPos;
Vector3 FromPos;

void Start()
{
    ToPos = transform.position + new Vector3(10, 0, 0);
    FromPos = transform.position - new Vector3(10, 0, 0);
}

// using Update since you are setting the transform.position directly
// Which makes me think you aren't worried about physics to much.
// Also FixedUpdate can run multiple times per frame.
void Update()
{
    // Using distance so it doesnt matter if it's x only, y only z only or a combination
    float range = Vector3.Distance(FromPos, ToPos);
    float distanceTraveled = Vector3.Distance(FromPos, transform.position);
    // Doing it this way so you character can start at anypoint in the transition...
    float currentRatio = Mathf.Clamp01(distanceTraveled / range + Time.deltaTime);

    transform.position = Vector3.Lerp(FromPos, ToPos, currentRatio);
    if (Mathf.Approximately(currentRatio, 1.0f))
    {
        Vector3 tempSwitch = FromPos;
        FromPos = ToPos;
        ToPos = tempSwitch;
    }
}
AresCaelum
  • 1,558
  • 1
  • 13
  • 18