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

Spaceships don't have reverse!

Discussion in 'Editor & General Support' started by Brian-Washechek, Jun 21, 2018.

  1. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    846
    I'm watching this tutorial and I made it through pretty well but the instructor allows his spaceship to backup. That may work for his game because it's top down but my spaceship game is like a side scroller. The spaceships in the game I'm working on should only go forward. How do I disable backwards movement?
    His movement code is like this:
    Code (csharp):
    1. pos += rot * velocity;
    I want to do something like this:
    Code (csharp):
    1. int velocityTest = int(velocity);
    2. if (velocityTest > 0)
    3.    pos += rot * velocity;
    however it's not liking that. Can someone tell me how I accomplish this?
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Too lazy to watch that tutorial, so by what you've written here and assuming you want to keep the general logic, just use Mathf.Clamp(velocity, 0f, 100f) or something like that.
     
  3. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    846
    I added that line wad Unity responded with
    Error CS1503 Argument 1: cannot convert from 'UnityEngine.Vector3' to 'float'
    to my addition of
    Code (csharp):
    1. Mathf.Clamp(velocity, 0f, 100f);
    How do I resolve this error? I know this is a simple question. Thank you for helping (if you do).
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Oh, I was assuming that velocity was a float but it seems it was a Vector3, that's why it didn't work.

    So I watched a bit of the tutorial.

    In a previous line where he sets velocity, instead of what I said in my previous post, do Mathf.Clamp01(Input.GetAxis("Vertical")), so it becomes like this:
    Code (csharp):
    1. Vector3 velocity = new Vector3(0, Mathf.Clamp01(Input.GetAxis("Vertical")), 0);
     
  5. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    846
    Ok, that works, and I thank you. But my game isn't very fun if you can't back up AT ALL. So I added a "crawl" type of reverse with
    Code (csharp):
    1. if (Input.GetAxis("Vertical") * maxSpeed * Time.deltaTime > 0)
    2. {
    3.      Vector3 velocity = new Vector3(0, Input.GetAxis("Vertical") * maxSpeed * Time.deltaTime, 0);
    4.       pos += rot * velocity;
    5.         }else{
    6.      Vector3 velocity = new Vector3(0, -(Input.GetAxis("Vertical") * maxSpeed * Time.deltaTime)/4, 0);
    7.       pos -= rot * velocity;
    8.  }
    It ma not be actually realistic but that's OK. It's a game. Partially we play games to escape reality.