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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I fix my script?

Discussion in 'Scripting' started by Treasureman, Jun 17, 2015.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a code that makes a target UI Image rotate in the direction of the movement axis. Here's the script...
    Code (JavaScript):
    1. var Arrow : UnityEngine.UI.Image;
    2.  
    3. function Update () {
    4. if (Input.GetAxis("Horizontal") < 0){
    5. Arrow.transform.rotation = Quaternion.Euler (0, 0, 90);
    6. }
    7. if (Input.GetAxis("Horizontal") > 0){
    8. Arrow.transform.rotation = Quaternion.Euler (0, 0, -90);
    9. }
    10. if (Input.GetAxis("Vertical") < 0){
    11. Arrow.transform.rotation = Quaternion.Euler (0, 0, 180);
    12. }
    13. if (Input.GetAxis("Vertical") > 0){
    14. Arrow.transform.rotation = Quaternion.Euler (0, 0, 0);
    15. }
    16.     }
    But, the problem is, it only rotate's it to those 4 angles. How would I make it rotate to all 360 degrees and not just -90, 0, 90, and 180?
     
  2. TheForsaken95

    TheForsaken95

    Joined:
    Aug 29, 2014
    Posts:
    30
    The only thing I could think of would be to increment the rotation by the Input.GetAxis() value. Since Input.GetAxis() returns a number between 0 and 1, you could multiply it by a speed variable, and then rotate it in increments every frame. I'm really new to this, so the code is probably all messed up. But hopefully it will give you some ideas on how to work out this problem.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Test : MonoBehaviour {
    5.  
    6.     public float rotationSpeed = 2.0f;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         float verticalPos = Input.GetAxis ("Vertical");
    16.         float horizontalPos = Input.GetAxis ("Horizontal");
    17.         verticalPos *= rotationSpeed;
    18.         horizontalPos *= rotationSpeed;
    19.         Arrow.transform.rotation = Quaternion.Euler (horizontalPos, verticalPos, 0);
    20.     }
    21. }
     
  3. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987