Search Unity

Switch between 2 lanes smoothly.

Discussion in 'Scripting' started by HariprasadA, Nov 14, 2017.

  1. HariprasadA

    HariprasadA

    Joined:
    Nov 14, 2017
    Posts:
    39
    This is my code to switch my player (who is a cube for the time being) between 2 lanes. However it creates a teleport kind of effect. Can someone help me out in smoothing the motion. I am a beginner so it would be kind if you could explain it a bit.

    Code-



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour {
    public float movedist=150;
    public float moveforce=300;
    private int playerpos = 1;
    private Rigidbody rb;
    public float switchspeed=300f;

    void Start()
    {
    rb = GetComponent<Rigidbody> ();

    }
    void FixedUpdate()
    {
    rb.AddForce (moveforce * Time.deltaTime, 0, 0);
    }
    void Update ()
    {
    if (Input.GetMouseButtonDown (0)) {
    if (playerpos < 2) {
    Vector3 pos = transform.position;
    Vector3 switch1 = new Vector3 (transform.position.x, transform.position.y, movedist);
    transform.position = Vector3.Lerp (pos, switch1, switchspeed);
    playerpos++;
    } else if (playerpos > 0) {
    Vector3 pos = transform.position;
    Vector3 switch2 = new Vector3 (transform.position.x, transform.position.y, 0);
    transform.position = Vector3.Lerp (pos, switch2, switchspeed);
    playerpos–;
    }
    }
    }
    }

    Thanks.
     
  2. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    You are having an error in how you use Vector3.Lerp(). It only takes values from 0 to 1 on the third parameter. Right now you are sending a 300, so it snaps to 1 and it teleports to the switch1 position with no transition. So what you want to do is a little math.

    Instead of using the speed of the transition, when you use Lerp you should use the time the transition will take. And then you need to divide the time that has passed since the last frame (Time.deltaTime) by the time you want it to take. So it will look like this.

    Code (CSharp):
    1. transform.position = Vector3.Lerp (pos, switch1, Time.deltaTime / transitionTime);
    Why that division, because Lerp takes a percentage 0-100 (used as 0.0f to 1.00f), so deltaTime / transitionTime gives you the percentage that deltaTime is to transitionTime. Sooo when enough frames have been called those small percentages will make the transition go to 1f, and it will take transitionTime seconds to get there.
     
  3. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    412
    I am not sure that is quite right. Assuming you are setting pos to transform before the lerp. You are always going to be lerping by about the same amount but the distance will be getting smaller and smaller. So it will slow down as it gets closer. Maybe you want this.

    I think what might of been better was to keep the start and destination points.
    Then each frame like whileandt said use delta time and the ration but like this.

    elapsedTime / transitionTime in your lerp and then
    Add elpasedTime += Time.deltaTime.

    But as your doing this in update with an GetMouseButtonDown around it and using rigid body with forces.
    I think you have a lot of other issues to look at.

    The input check will only happen when down.
    Then it will only happen maybe two times because of your playerpos variable.
    So you willl never get a smooth movement.

    Maybe explain more what you want to do and then can give better advice.
    And post your code in code tags.
     
  4. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    353
  5. HariprasadA

    HariprasadA

    Joined:
    Nov 14, 2017
    Posts:
    39
    Real fast replies! Will try em all! Thanks guys...
     
  6. HariprasadA

    HariprasadA

    Joined:
    Nov 14, 2017
    Posts:
    39
    That worked 95% but it still creates a kind of weird shake on the player like for a blink of an eye.Why does that happen?
    I changed the input to A and D to switch. Anyway thanks for your help.
     
  7. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201