Search Unity

GameObject rotate

Discussion in '2D' started by meierdesigns, May 27, 2017.

  1. meierdesigns

    meierdesigns

    Joined:
    Aug 3, 2016
    Posts:
    22
    Hey,
    I want to create a snake game, but the objects rotate in a way, so they won't be displayed anymore.

    The code of the controller is:

    Code (CSharp):
    1. using UnityEngine;[/I]
    2. [I]
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6.  
    7.  
    8.  
    9.  
    10. public class Snake : MonoBehaviour
    11. {
    12.    float timer;
    13.    float timelaps = 20;
    14.     int direc;
    15.    // Did the snake eat something?
    16.     bool ate = false;
    17.    float x = 0.1F;
    18.    float y = 0.1F;
    19.    public GameObject GameOver;
    20.    // Tail Prefab
    21.     public GameObject tailPrefab;
    22.     // Head Prefab
    23.     public GameObject head;
    24.  
    25.  
    26.     // Current Movement Direction
    27.     // (by default it moves to the right)
    28.     Vector2 dir = Vector2.down;
    29.  
    30.     // Use this for initialization
    31.     void Start()
    32.     {
    33.         // Move the Snake every 300ms
    34.        //InvokeRepeating("Move",x, y);
    35.     }
    36.  
    37.     // Update is called once per Frame
    38.     void Update()
    39.     {
    40.         // Move in a new Direction?
    41.         //head.transform.rotation = Quaternion.AngleAxis(30, Vector3.up);
    42.         float step = Time.deltaTime;
    43.  
    44.         if (Input.GetKey(KeyCode.UpArrow) && dir != Vector2.down)
    45.         {
    46.             dir = Vector2.up;
    47.             head.transform.Rotate(Vector2.up, 90);
    48.         }
    49.         else if(Input.GetKey(KeyCode.DownArrow) && dir != Vector2.up)
    50.         {
    51.             dir = Vector2.down;
    52.             head.transform.Rotate(Vector2.down, 90);
    53.         }
    54.         else if (Input.GetKey(KeyCode.RightArrow) && dir != Vector2.left)
    55.         {
    56.             dir = Vector2.right;
    57.             head.transform.Rotate(Vector2.right, 90);
    58.             //head.transform.rotation = Quaternion.AngleAxis(30, Vector2.right);
    59.         }
    60.         else if (Input.GetKey(KeyCode.LeftArrow) && dir != Vector2.right)
    61.         {
    62.             dir = Vector2.left;
    63.             head.transform.Rotate(Vector2.left, 90);
    64.             //head.transform.rotation = Quaternion.AngleAxis(30, Vector2.left);
    65.         }
    66.      
    67.        if (timer >= timelaps) {
    68.            Move();
    69.            timer = 0;
    70.        }else{
    71.            timer++;
    72.        }
    73.     }
    74. }


    Here's a screenshot of the scene:

    What rotation method should I use, to rotate the sprite of the game object?
    Wrong_rotation.PNG

    A: The rotated object
    B: This is the rotation,
    C: This variable needs to be rotated.
    Please help me to finish the rotation :D

    Best regards
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Answer 'C'. I'd go with
    Code (csharp):
    1.  transform.rotation = Quaternion.Euler(0,0,angleVal); // where angleVal is your desired degrees.
    I'm most comfortable with that call. Pretty sure angleAxis would work , also.
    You can try it on the sprite, just click sort of where the 'z' is in the inspector with your mouse and drag side-to-side and you should see it rotating ;)
     
    meierdesigns likes this.
  3. meierdesigns

    meierdesigns

    Joined:
    Aug 3, 2016
    Posts:
    22
    Hey that's really awesome :D
    Thank you so much.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem :) You're welcome.