Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Moving player between pre-determined points.

Discussion in '2D' started by Gamesatan, May 28, 2017.

  1. Gamesatan

    Gamesatan

    Joined:
    May 28, 2017
    Posts:
    3
    I posted this to unity answers a while ago, but after reading up on the "waitin for moderation" limbo i tought i might as well post my question here while waiting.

    I'm new to unity and have some questions for you guys.

    I'm creating a 2d game in which you move a rabbit between 3 lanes in order to avoid falling blocks. I already have a character with animation and collision standing on a sprite with a box collider that acts as ground.

    So basically i would like to know how to move my rabbit left and right between 3 points using arrow keys. I found plenty of tutorials online in how to make a character move around, but nothing regarding this. Or maybe i just don't know what to google.

    Here's a picture to further illustrate my problem since my english isn't the best. https://up.ppy.sh/files/721341236123145.png
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    In a scene like that with just the 3 spots on the ground, you might try having 3 small colliders/triggers (1 at each spot).
    Then, setting the velocity of the rigidbody to move with the right speed & in the desired direction, just use the collision/trigger to stop the character (set velocity to Vector3.zero). :)
     
  3. Gamesatan

    Gamesatan

    Joined:
    May 28, 2017
    Posts:
    3
    Seems like a good idea, i'll look into that. This however raises a new questions.

    I'was able to make my rabbit move left and right with GetKey and Vector3, but what i can't figure out is how to make him move indefinetly with a single keystroke, so that he would move until hitting the trigger.

    Then i could just loop the walking animation while he moves and revert to idle when he hits a trigger.
    Does that make any sense?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yes, that makes sense as you asked it. If you set the velocity vector based on the input key, it will remain "set/on" until you tell it otherwise (eg: setting it to Vector3.zero and setting the idle animation when it hits the trigger). Since your character shouldn't "only go part way" in between the spots, that seems fitting =)
     
  5. Gamesatan

    Gamesatan

    Joined:
    May 28, 2017
    Posts:
    3
    Hello again, i tought i'd bump this thread in case you wanted to help me out a little more.
    So basically this is what i've been able to cobble up so far.

    A script that moves player
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pcontroller : MonoBehaviour {
    6.  
    7.     public float velocity;
    8.     public Rigidbody2D rb;
    9.  
    10.     void Start () {
    11.         rb = GetComponent<Rigidbody2D> ();
    12.     }
    13.  
    14.     void FixedUpdate () {
    15.         if (Input.GetKeyDown (KeyCode.D)) {
    16.             rb.velocity = new Vector3 (1, 0, 0) * velocity;
    17.             }
    18.         if (Input.GetKeyDown (KeyCode.A)) {
    19.             rb.velocity = new Vector3 (-1, 0, 0) * velocity;
    20.  
    21.         }
    22.     }
    23. }
    It works great, but the promblem is that sometimes it takes multiple keypresses to make the player move.
    If i put the code in Update instead of FixedUpdate it works flawlessly, but then the problem is that i can change direction mid movement, which i of course don't want to happen since player should only stop once colliding with a trigger.

    I also have a script for the trigger, but i can't figure out how to refer to the public rigidbody to zero out the velocity it has.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class triggerscript : MonoBehaviour {
    6.  
    7.  
    8.  
    9.     void OnTriggerEnter2D(Collider2D col) {
    10.         if (col.gameObject.tag == "Player") {
    11.             rb.velocity = new Vector3 (0, 0, 0);
    12.         }
    13.     }
    14. }
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, I'll work backwards through the issues. You could put the trigger code on the player object. Of course that's just an option, it's not required. In order to lookup the rigid body on a collider (from 1 script to a different object), you'd use:
    Code (csharp):
    1.  
    2. // inside the collision, after you check its tag
    3. col.GetComponent<Rigidbody>().velocity = // etc
    4.  

    Okay, now the moving part. Update is definitely the way to go. Fixed Update isn't reliable for input (with the exception of 'GetKey' or anything that's trying to read a constant down .. if that makes sense. As compared to Down/Up which are 1 time events -- this is because you can miss an event in Fixed Update, sometimes.
    You can set a bool to tell you that you're moving, and disallow further inputs until they've collided.
    You can reset the bool to false upon collision :)

    Also very small note, barely worth mentioning, but you could change this:
    Code (csharp):
    1.  rb.velocity = new Vector3 (-1, 0, 0) * velocity;
    2. //to this:
    3. rb.velocity = new Vector3 (-velocity, 0, 0);
    and the other (positive direction) one , too.

    Hope that helps.