Search Unity

Inverse direction of rotation

Discussion in 'Scripting' started by crawl55, Dec 9, 2018.

  1. crawl55

    crawl55

    Joined:
    Jan 17, 2014
    Posts:
    8
    My script, as it stands now displays the value in the opposite direction that I want.

    I have a boat which rotates when you press the left or right key...

    When it rotates to the left, the value received starts at 0 and increments up from there to 360.

    I want it to start at 0 and increment to 360 when it rotates to the right. The script I am using is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System;
    6.  
    7. public class DisplaySpriteRotation : MonoBehaviour
    8. {
    9.     public GameObject Rigidbody2D;
    10.     public GameObject Object;
    11.     public static float degrees; // Used to determine heading
    12.  
    13.     // Use this for initialization
    14.  
    15.     void Start ()
    16.     {
    17.         Rigidbody2D = GameObject.Find("Character");
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update ()
    22.     {
    23.         print((degrees) + "degrees");
    24.  
    25.         degrees = gameObject.transform.rotation.eulerAngles.z; // Get heading of 2D Sprite
    26.         degrees = Mathf.Round(degrees);// round heading to whole number
    27.         degrees = degrees / 360; // prints value as 0 - 1
    28.        
    29.         // if (degrees >= 0f && degrees <= 4.23f) { this.transform.Find("sprite1").GetComponent<SpriteRenderer>().enabled = true; }
    30.     }
    31. }
    How can I reverse the result? Thank You.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    I'm not sure what this code you're showing has to do with your issue.

    This code doesn't turn your ship... there's nothing reading for an input of left or right. It doesn't set the rotation value anywhere. Maybe show us that code rather than this code?

    As for how to accomplish it?

    Have you tried multiplying by -1?
     
    chubbspet likes this.
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    There are a few things that may be worth bearing in mind.

    Remember that all viewpoints are relative. If you are 'above' an object looking 'down' and it rotates clockwise, then moving to be 'beneath' it looking 'up' will make it appear to rotate anti-clockwise.

    So, three immediate options are (i) move the camera, (ii) rotate the ship in the opposite direction or (iii) adjust your end result by multiplying by -1 (as lordofduct suggested).

    Also, if you are using a sprite, have you set it to 'flip' its display? So maybe it is rotating correctly but just displaying inverted.