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. Dismiss Notice

Simple Rotation

Discussion in 'Scripting' started by QTate, Apr 7, 2017.

  1. QTate

    QTate

    Joined:
    Oct 25, 2015
    Posts:
    22
    Hello,

    I've done some research online and it's helped me understand Transform and Rotation, however I'm still having some issues with getting a Cube GameObject to flip on it's side if the W,A, S, D keys are pressed. I was wondering if this is even possible? I've included my script and was needing some advice or feedback from someone who'd be willing to help explain or point me in the right direction.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Rotate : MonoBehaviour
    {

    public KeyCode pressUp;
    public KeyCode pressDown;
    public KeyCode pressLeft;
    public KeyCode pressRight;



    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {
    if (Input.GetKeyDown (pressUp))
    {
    GetComponent<Transform> (). eulerAngles = new Vector3(0,0,0);
    }
    if (Input.GetKeyDown (pressDown))
    {
    GetComponent<Transform> (). eulerAngles = new Vector3(0,0,180);
    }
    if (Input.GetKeyDown (pressLeft))
    {
    GetComponent<Transform> (). eulerAngles = new Vector3(0,0,90);
    }
    if (Input.GetKeyDown (pressRight))
    {
    GetComponent<Transform> (). eulerAngles = new Vector3(0,0,-90);
    }

    }
    }
     
  2. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    lordofduct likes this.
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    In addition to code tags, could you describe the behaviour you're seeing at the current time? :)
     
  4. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    I was replying to your thread on the General forum, but it seems to have been deleted, so here's what I was going to say for the question as it was posed in that thread (this thread's question isn't clear on what the problem is; is it an instant flip or a continuous rotation you want?)

    GetKeyDown only fires once for each key press, use GetKey() to check continuous key hold.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    In that particular code, it would make no (apparent) difference; it's just flat-out setting the rotation, not modifying the rotation by a particular amount.
     
  6. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Aye, the code in the deleted thead was +=, but I didn't look at this threads that closely.