Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

How To Translate GameObject To Where It's Facing ?

Discussion in '2D' started by bernhardoj, Feb 14, 2018.

  1. bernhardoj

    bernhardoj

    Joined:
    Dec 22, 2017
    Posts:
    38
    Hi, I'm trying to translate my player every time the button is pressed.

    This is script I used :
    Code (CSharp):
    1. player.transform.Translate (new Vector2 (5f, 0f));
    But my player always be translated to right..

    How to make my player be translated to where he's facing ?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,710
    That is the proper code to move 5 to the right, based on 'facing'.
     
  3. bernhardoj

    bernhardoj

    Joined:
    Dec 22, 2017
    Posts:
    38
    But I want when my player face left, he will be translated to x = -5f (left).
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,710
    I see. Well, if it was rotated it would be. Perhaps you are turning by negative scale or flipX ?

    for negative scale:
    Code (csharp):
    1. player.transform.Translate (new Vector2 (Mathf.Sign(player.transform.localScale.x) * 5f, 0f));
    It's basically what you had before, but multiplies your movement value by 1 or -1, depending on the value of the localScale's 'x'.

    If it's flipX you're using and can't work it out, I can try to help you with an example.
     
  5. bernhardoj

    bernhardoj

    Joined:
    Dec 22, 2017
    Posts:
    38
    Thanks very much it's worked. Now I have a new problem. My Player can be Translated through wall. How I can fix it so if there is a wall it will just Translated exactly at the edge of the wall?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,710
    Well, in 2D you can either try to raycast before you move to determine if the move is valid, or you can use physics and move with rigidbody. Some people like to try making their own collision detection, but I think using physics is a lot easier.
    Physics will/would require you to change your code so you move with the system; instead of modifying the transform position directly (as in translate), you set the velocity of the rigidbody or addforce to it.
     
  7. bernhardoj

    bernhardoj

    Joined:
    Dec 22, 2017
    Posts:
    38
    Hmm, i'm sorry i can't understand what u r trying to explain. I'm still new to raycast and physics. Can you give me some example ?
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,710
    Well, as I suggested you could always try using physics instead.

    Please feel free to look this topic up online, because I'm sure there are many posts on raycasting in 2d.
    Then, if you had a question about something you've tried, perhaps me or someone else can try to assist you.

    The docs that explain, describe/show it: https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
    You can also cast "shapes" : box, circle,capsule (and the 3d equivalents).

    Physics (rigidbody2d): https://docs.unity3d.com/Manual/class-Rigidbody2D.html
    and: https://docs.unity3d.com/ScriptReference/Rigidbody2D.html

    Hopefully that in combination with whatever you can search for online can help you get started.
     
    Last edited: Feb 17, 2018
  9. bernhardoj

    bernhardoj

    Joined:
    Dec 22, 2017
    Posts:
    38
    Thanks, I will try to learn it and will ask if I need something..