Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Move sprite forward in Space.Self

Discussion in 'Scripting' started by Temp10101, Apr 19, 2015.

  1. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    Currently attempting something that looks like this:
    Code (CSharp):
    1.  
    2.         if (Input.GetKey (KeyCode.W)) {
    3.             Debug.Log ("Reaction!");
    4.             transform.position += new Vector2(0, 5, Space.Self);
    5.         }
    But it doesn't take more than 2 arguments. What I'm trying to do, is make sprite move forward to it's own relative position. I have other script, which turns around the player, and actually changes sprite's rotation, I try to make it go in the direction of cursor. But I can't figure out how. Vector2.up, makes it go infinitely to the top, literally, but not in direction of cursor.

    by the way, Space.Self is apparently not part of Vector2, what is it part of then? I forgot.
     
  2. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    233
    Vector2 only takes 2 aguments, x, and y.
    Vector3 takes 3 arguments, but the 3rd one is the Z depth, and I doubt you want that for a 2d game.

    You can use transform.forward to get the facing position of the object, and transform.right to get the right side, etc. These are always the forward and right sides of the object, even when it's rotated. Try em out!

    Code (CSharp):
    1. if (Input.GetKey (KeyCode.W)) {
    2.             Debug.Log ("Reaction!");
    3.             transform.position += transform.forward * 5
    4. }
     
  3. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    transform.forward got me WAY out in the Z, while my 2D game is YZ, I changed it to transform.position += transform.right / 10; and it worked just great.