Search Unity

Jumping side to side help

Discussion in 'Scripting' started by Robster95, Oct 5, 2020.

  1. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    I'm working on a small project while taking a break from my bigger project and I ran into a problem of having the character jump side to side.

    I want to try to make an endless runner type game like a mix of temple run and geometry dash where the player is running down a path with obstacles and can either be in the center of the lane, the left or the right.

    The problem i'm having is I was the player to be able to move between the three spots. Left, center, or right. But i'm not trying to have the player just appear in the new positions I would like for the gameobject to slide or move there.

    If anyone is able to help I would greatly appreciate it.

    Ex. I'm not trying to us transform.translate and set the position because it just appears in the spot. I would like the user to press a button and the player slides to a set destination
     
  2. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    i can´t try it atm, but shouldn´t it work with something like that:


    Code (CSharp):
    1.  
    2. float movementSpeed = 2;
    3.  
    4. if (Input.GetKey(KeyCode.RightArrow))
    5. {
    6.     float centerRight; // Set here the mid of your right side
    7.    
    8.     Vector3 newPosition = new Vector3(transform.postion.x, centerRight, transform.postion.z);
    9.    
    10.     // Check if the palyer is already on the right side
    11.     if (transform.position.y != centerRight)    
    12.         // Move your player to the center of the rifht side
    13.         transform.postion = Vector3.Lerp(transform.postion, newPosition, Time.deltaTime * movementSpeed);
    14. }
    And the same for the left side.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    I would start a coroutine when you detect the player input. Inside the coroutine, do the movement over the course of multiple frames using yield return null in a loop.