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

2D Constant Movement When Key Is Released

Discussion in 'Scripting' started by AnnieAnn123, Jul 28, 2015.

  1. AnnieAnn123

    AnnieAnn123

    Joined:
    Jul 28, 2015
    Posts:
    7
    I'm fairly new to game creation, but thought I'd give it a try.

    http://s29.postimg.org/tg048n2xz/Question1.png

    I thought it would be better to draw a quick picture, with my question. I'm not too good at explaining in words.

    Thanks,
    Ann
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Which aspect are you struggling with? What code have you written?

    We'll be happy to help guide you but we're not interested in writing whole scripts - you'll never learn that way.
     
  3. AnnieAnn123

    AnnieAnn123

    Joined:
    Jul 28, 2015
    Posts:
    7
    Code (CSharp):
    1. float timer;
    2. Vector3 startPos;
    3. Vector3 endPos;
    4.  
    5. void Start()
    6. {
    7.    RandomPosition();
    8. }
    9.  
    10. void RandomPosition()
    11. {
    12.   timer = Time.time;
    13.   startPos = transform.position;
    14.   endPos = new Vector3(0, (startPos.y + 2.0f), 0);
    15. }
    16.  
    17. void Update()
    18. {
    19.   if (Input.GetKeyUp("space")) {
    20.        if (Time.time - timer > 1)
    21.       {
    22.          RandomPosition();
    23.        }
    24.        transform.position = Vector3.Lerp(startPos, endPos, Time.time - timer);
    25.    }
    26. }
    But this doesn't move the object at a constant speed, 2.0f units up. And it'll only move the object if 1 second is elapsed.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Instead of using Lerp, use Vector3.MoveTowards. Then you can set a constant speed.

    You also probably want to move that line outside of the if statement (to the very bottom of Update), so it runs all the time instead of only when you release the spacebar.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're move code is within the scope controlled by the input check....i.e the object only moves in the frame the space bar is released.

    move line 24 below the brace on line 25 for it to always move towards the end pos. This will allow the space to change the end position, you might want to add a check that the transform is within a small distance of the end pos if you want to make sure it makes it is close to the point before allowing another one to be set.

    oh and adding an or clause (&& = and, || = or) of "or if there is no end pos" will allow it to work within the first second, be careful with the brackets on that though :)
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    I took your script and sorta changed it around a bit. I think this is closer to what you want. I put a sphere in my scene instead of a sprite but the point is the same. Here is the changed script, but the entire scene is attached too.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class RandomMover : MonoBehaviour
    6. {
    7.     public float speed = 5.0f;
    8.  
    9.     Vector3 goalPos;
    10.  
    11.     void Start()
    12.     {
    13.         goalPos = transform.position;
    14.     }
    15.  
    16.     void RandomPosition()
    17.     {
    18.         goalPos = new Vector3(0, (transform.position.y + 2.0f), 0);
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (Input.GetKeyUp("space"))
    24.         {
    25.             RandomPosition();
    26.         }
    27.         Vector3 delta = goalPos - transform.position;
    28.         float step = speed * Time.deltaTime;
    29.         if (delta.magnitude >= step)
    30.         {
    31.             transform.position += delta.normalized * step;
    32.         }
    33.     }
    34. }
    The undefined behavior here is if you are already in transit, then press/release SPACE, it moves you to y = +2 from the precise place you were at that moment.
     

    Attached Files:

  7. AnnieAnn123

    AnnieAnn123

    Joined:
    Jul 28, 2015
    Posts:
    7
    Thanks, everyone for the help :)

    This script works perfectly, but is there anyway to disable the spacebar while the object is moving?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Given that:

    a) the object moves until it reaches its destination
    b) we already have a check that checks if it still needs to keep moving

    Then we can trivially make an else block that only ALLOWS the spacebar when you are "close enough" to the goal position.... kinda like so:

    Code (csharp):
    1.     void Update()
    2.     {
    3.         Vector3 delta = goalPos - transform.position;
    4.         float step = speed * Time.deltaTime;
    5.         if (delta.magnitude >= step)
    6.         {
    7.             transform.position += delta.normalized * step;
    8.         }
    9.         else
    10.         {
    11.             if (Input.GetKeyUp("space"))
    12.             {
    13.                 RandomPosition();
    14.             }
    15.         }
    16.     }
     
  9. AnnieAnn123

    AnnieAnn123

    Joined:
    Jul 28, 2015
    Posts:
    7
    thank you!!!