Search Unity

Having Control of Start point for Lerp Object

Discussion in 'Scripting' started by shellac85, Jul 18, 2017.

  1. shellac85

    shellac85

    Joined:
    Mar 29, 2016
    Posts:
    23
    In the code below, how to I have control of the X axis position of the object. Right now it always starts form the centre. I want to have many objects moving and want to control their X axis positon or even make it random each time. Any help appreciated. :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PillarMovement : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.  
    9.     public Transform startMarker;
    10.     public Transform endMarker;
    11.     public float speed = 1.0F;
    12.  
    13.     private float startTime;
    14.     private float journeyLength;
    15.  
    16.  
    17.  
    18.  
    19.     void Start ()
    20.     {
    21.        
    22.         startTime = Time.time;
    23.         journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
    24.  
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void Update ()
    29.     {
    30.         float distCovered = (Time.time - startTime) * speed;
    31.         float fracJourney = distCovered / journeyLength;
    32.         transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
    33.        
    34.     }
    35. }
    36.  
     
  2. Kasper_OmsK

    Kasper_OmsK

    Joined:
    Jul 9, 2015
    Posts:
    47
    I don't quiet get what you want.

    What do you mean by "it always starts form the center" ?

    You want the lerp to occure but still control how the x component evolves over time ?
    Then it's not a lerp. A lerp is an linear interpolation, as the name says. The only movement possible by this technique is a straight line.

    If you want your transform to follow another path, you'll have to make it yourself.

    Or maybe I didn't understand what you meant.
     
  3. shellac85

    shellac85

    Joined:
    Mar 29, 2016
    Posts:
    23
    Hi, I mean if the object is on a plane(the ground) it seems to start from the middle point of the plane, whereas I want a few other objects to start from the left and right of the centre
     
  4. Kasper_OmsK

    Kasper_OmsK

    Joined:
    Jul 9, 2015
    Posts:
    47
    Oh, yes ok.
    I didn't really went through your code at first.

    The issue probably has to do with line 32

    Code (CSharp):
    1.  transform.transform.Lerp(startMarker.position, endMarker.position, fracJourney);
    Lerp returns the point which lands on straight line at the percentage given in 3rd parameter.
    Typically, Lerp is not used like you did. You use a fancy way to calculate that percentage over time but generally you'd do something like this :

    Code (CSharp):
    1.  transform.position = Vector3.Lerp(transform.position, endMarker.position, 0.1f); //0.1f being the percentage of the total distance "travelled" during each update
    Of course what you'd do is first set the position at your startMaker.position and then use the Lerp as I did.

    EDIT : NVM I'm stupid. What you did should work fine. But I still don't get what you want...
    If you want to have a random start position, then @jeffreyschoch explained it.
     
    Last edited: Jul 18, 2017
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    If you want to randomize the starting position, you can add an offset to your startMarker.position.

    Maybe like this?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PillarMovement : MonoBehaviour
    5. {
    6.     public Transform startMarker;
    7.     public Transform endMarker;
    8.     public float randomOffsetAmount;
    9.     public float speed = 1.0F;
    10.  
    11.     private float startTime;
    12.     private float journeyLength;
    13.     private Vector3 startPosition;
    14.  
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         startTime = Time.time;
    19.         startPosition = startMarker.position + (Vector3.right * Random.value * randomOffsetAmount);
    20.         journeyLength = Vector3.Distance(startPosition, endMarker.position);
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         float distCovered = (Time.time - startTime) * speed;
    27.         float fracJourney = distCovered / journeyLength;
    28.         transform.position = Vector3.Lerp(startPosition, endMarker.position, fracJourney);
    29.     }
    30. }
     
  6. shellac85

    shellac85

    Joined:
    Mar 29, 2016
    Posts:
    23
    Hi sorry for my late reply, I am only getting to try this out now. It works great. Thanks for your help :)
     
    LiterallyJeff likes this.