Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Player Movement Lerp

Discussion in 'Scripting' started by Deleted User, Jun 23, 2016.

  1. Deleted User

    Deleted User

    Guest

    Hello, i'm making an android 2d game, i have a problem making the movement, what i want to do is make it that when you shoot bellow yourself / on top of yourself / any side etc. as long as it hits a wall in like some radius it moves you in the opposite way from the rocket (U will understand what i'm trying to make if u have played rocket jump in csgo :D). Like a blast that throws you away, i tried to do it this way: Get the pos from place where rocket hit's ground, multiply it by -1 so it's the opposite, and then using Lerp() move the player to that position. But instead of smoothly moving to that position it jumps there instantly or jumps instantly in like midway of the end position (Depends on the t float).

    Maybe there is a better way to do it?

    Here's my code:
    Code (CSharp):
    1. public GameObject Player;
    2.     public GameObject Rocket;
    3.     public GameObject MovePosIndicator;
    4.     public bool rocketDetected;
    5.     public float MoveDistanceX;
    6.     public float MoveDistanceY;
    7.     public float MoveSpeed;
    8.     public Vector2 MovePosition;
    9.  
    10.     void Start () {
    11.         Player = GameObject.Find ("Player");
    12.     }
    13.  
    14.     void Update () {
    15.  
    16.         if (Rocket == null) {
    17.             rocketDetected = false;
    18.         }
    19.         if (Rocket != null) {
    20.             MoveDistanceX = Rocket.transform.position.x - transform.position.x;
    21.             MoveDistanceY = Rocket.transform.position.y - transform.position.y;
    22.  
    23.             MovePosition.x = MoveDistanceX * -1;
    24.             MovePosition.y = MoveDistanceY * -1;
    25.  
    26.             if (Rocket.GetComponent<Rocket> ().rocketBlewUp) {
    27.                 MovePosIndicator.gameObject.transform.position = MovePosition;
    28.  
    29.                 Player.transform.position = Vector3.Lerp(Player.transform.position, MovePosition, MoveSpeed * Time.deltaTime);
    30.             }
    31.         }
    32.  
    33.     }
    34.  
    35.     void OnTriggerEnter2D(Collider2D other){
    36.         if (other.gameObject.tag == "PlayerDetector") {
    37.             if (other.transform.parent.gameObject.tag == "RocketShot") {
    38.                 Rocket = (other.transform.parent.gameObject);
    39.                 rocketDetected = true;
    40.             }
    41.         }
    42.     }
    43.  
    44.     void OnTriggerStay2D(Collider2D other){
    45.         if (other.gameObject.tag == "PlayerDetector") {
    46.             if (other.transform.parent.gameObject.tag == "RocketShot") {
    47.                 Rocket = (other.transform.parent.gameObject);
    48.                 rocketDetected = true;
    49.             }
    50.         }
    51.     }
     
    Last edited by a moderator: Jun 23, 2016
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    You're using Lerp wrong!

    Check out the "Lerp like a pro" link in my signature for how to use lerp properly.
     
    Deleted User likes this.
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    You seem to have a host of problems.

    1, you are using a Vector2 to handle your position, but you are using Vector3.Lerp with it, that should not work out right for you
    2. you seem to not be letting Unity help you out at all with math
    Code (csharp):
    1.  
    2. MovePosition = transform.position - Rocket.transform.position;
    3.  // not
    4. MoveDistanceX = Rocket.transform.position.x - transform.position.x;
    5. MoveDistanceY = Rocket.transform.position.y - transform.position.y;
    6.  
    7. MovePosition.x = MoveDistanceX * -1;
    8. MovePosition.y = MoveDistanceY * -1;
    9.  
    3.You should examine why you are using Lerp here in the first place.
    My guess is that you want to use Lerp to move your player towards the rocket.
    To do this, you dont use Lerp at all, you use MoveTowards. ;)
    Code (csharp):
    1.  
    2. Player.transform.position = Vector3.MoveTowards(Player.transform.position, MovePosition,MoveSpeed * Time.deltaTime );
    3.  
     
    Deleted User likes this.
  4. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    [JOKE]
    This should be implemented as a warning message anytime Time.deltaTime is used as parameter with Lerp.
    [/JOKE]
     
    Last edited: Jun 23, 2016
    Deleted User likes this.
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Why? it's perfectly valid to use lerp with deltatime, assuming that's the behaviour you're going for. Perhaps some people desire a curve.
     
    Deleted User likes this.
  6. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    @hippocoder
    About the OP implementation:
    Code (CSharp):
    1. Player.transform.position = Vector3.Lerp(Player.transform.position, MovePosition, MoveSpeed * Time.deltaTime);
    If you let yourself being mislead by the variable naming, you would think that MoveSpeed*Time.deltaTime is a step for the current frame. Therefore you can conclude that the intention is to move towards MovePosition at constant speed, which is not what this line will effectively do.

    More generally, this way of using Lerp is for doing some damping.
    Most of the time Lerp is used with Time.deltaTime is to implement some damping towards a target position:
    Code (CSharp):
    1. transform.position = Vector3.Lerp(transform.position, targetPosition, dampingFactor* Time.deltaTime);
    When that is used in Update, the presence of Time.deltaTime might mislead you into thinking that the damping is frame-rate-independent, which is also wrong: the damping will be slower with a lower framerate. (Thought, this is not the case with FixedUpdate since Time.deltaTime is constant).

    Above all, there is a method to do damping properly:
    https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html
     
    Last edited: Jun 23, 2016
    Deleted User likes this.
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yeah - but throwing an error for use of DeltaTime is a bit out there & impossible to police :D
     
    Deleted User likes this.
  8. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    That was intended as a joke (I've updated my post with [JOKE]), not an actual feature suggestion.

    But considering how frequent this way of using lerp is, it seems that there is something to be done.
     
    Deleted User likes this.
  9. Deleted User

    Deleted User

    Guest

    Uh, thank you all! I feel so stupid right now tho' :D And the main problem was that i make it do the lerp only like in 1 frame... Bcz of this:
    Code (CSharp):
    1.  if(Rocket !=null){
    2.      if(Rocket.GetComponent<Rocket>().rocketBlewUp){
    3.           //~1 frame
    4.      }
    5. }