Search Unity

How to flip an object around an axis from a keypress?

Discussion in 'Scripting' started by ShodanCat, Mar 3, 2015.

  1. ShodanCat

    ShodanCat

    Joined:
    Mar 3, 2015
    Posts:
    3
    First off I'm a beginner with Unity, and I apologize if this is obvious, but despite many, many searches and reading I'm not having any luck.

    All I'm trying to do is flip an object depending on a keypress. So for example, if I press 'a' the object (character model in this case) will point to the left, and if I press 'd', the object will face to the right.

    This is using C#, btw.

    In other words, what do I put:

    if(Input.GetKey("d"))
    {
    //here!
    }

    To rotate on object to face either left or right.

    Thanks a lot in advance!
     
  2. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    Depends on what context you're refering to, but from what it sounds like, I'd look into rotating your character's transform based on your input. Meaning when you press a, you rotate 90 degrees in the Y direction one way, and press D you rotate 90 degress in the Y direction the other way (180 from when you press A).


    Rather than just giving you the answer, I propose that I give you where to start looking. Additionally if you're using unity it is always helpful to look into basic C# tutorials which you can find on youtube. I'd suggest starting with these functions though as I believe they are going to be what you're looking for in the Unity API. Found here <http://docs.unity3d.com/ScriptReference/>.


    Transform
    Transform.rotation
    GameObject.GetComponent<Type>()
    Input.GetKey and its variations
    Input.GetAxis (also useful)
    Quaternion (may want to youtube this)
    Transform.LocalEulerAngles

    As they say give a man fish, feed him for a day, teach a man to fish, hope he doesnt catch all your stinkin fish.... ANYWAY give it a shot, if you're still struggling post what you come up with and ill help you a bit more.
     
  3. ShodanCat

    ShodanCat

    Joined:
    Mar 3, 2015
    Posts:
    3
    Thanks, but those functions are what I've been struggling with for literally hours now. I know my post makes it sound like I know absolutely nothing and am hoping to just get a piece of code and move on, but after reading and reading, testing and testing, I'm not able to get anything useful.

    For example, here's what I'm trying at the moment:

    Code (CSharp):
    1.         if(Input.GetKey("d"))
    2.         {
    3.             transform.localScale = new Vector3(1.0f, transform.localScale.y, transform.localScale.z);
    4.         }
    5.         if(Input.GetKey("a"))
    6.         {
    7.             transform.localScale = new Vector3(-1.0f, transform.localScale.y, transform.localScale.z);
    8.         }
     
  4. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    Have you declared a variable and assigned it the transform?

    When using a component in Unity 5, you must declare a variable of that type, then use GetComponent<Type>() to assign the variable.

    E.G.

    Transform trans;

    void Start(){
    trans = GetComponent<Transform>();
    }

    Then instead of transform.localScale in your if statement you would use trans.localScale
     
  5. ShodanCat

    ShodanCat

    Joined:
    Mar 3, 2015
    Posts:
    3
    Well, for starters I am using Unity 4 still. Here is what I've come up with as of right now that partial works. This code faces the character left when I press "a", but for some reason refuses to flip again when I press "d". I have a debug message showing me the transform.eulerAngles.y so I can make sure my condition is met, but for whatever reason it's not being met.

    Code (CSharp):
    1. public class FaceMovement : MonoBehaviour
    2. {
    3.     void Update()
    4.     {
    5.         Debug.Log (transform.eulerAngles.y);
    6.  
    7.         if(Input.GetKey ("a") && transform.eulerAngles.y == 90.0f)
    8.         {
    9.             transform.Rotate (Vector3.up, 180.0f);
    10.         }
    11.  
    12.         if(Input.GetKey("d") && transform.eulerAngles.y == 270.0f)
    13.         {
    14.             transform.Rotate (Vector3.up, -180.0f);
    15.         }
    16.     }
    17. }
    18.  
     
  6. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Additionally to comments posted,
    Have a look at the difference between GetKey, GetKeyDown and GetKeyUp
    http://docs.unity3d.com/ScriptReference/Input.html

    GetKey will respond for each frame that you have the key pressed ( ie 30 odd times per second) ;
    whereas the others will fire when the key is pressed or released only.

    Do you want the hero to make one quarter turn or to be a corkscrew ??
     
    CastleIsGreat likes this.