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

Worms/Scorched Earth/Gunbound - Angle Selection (For my game "UbberStory")

Discussion in 'Scripting' started by ubberkid, May 20, 2014.

  1. ubberkid

    ubberkid

    Joined:
    Mar 11, 2014
    Posts:
    17
    :confused::confused::confused:
    So my game is going to be just like the aforementioned games in the title.
    In all of these games, they have an angle that you can select.
    The angle will then be used to shoot projectiles in the selected angle direction.
    I need some advice or a point in the right direction to acquire the same effect.​
    [HR][/HR]

    So basically, 90 degrees would be straight up, if you change your angle to the right or left it would go down from 90. ex. 89 ,88, 87 etc.

    I will have some text next to the needle displaying what angle is currently selected.
    Also, i would need it to do the following:
    For ex. You are going UP a slope. You are facing right with an angle of 75 degrees, when you turn left and head DOWN the slope the angle is obviously not going to be the same because the "needle" is now facing lower because of the slope.
    I need it to know what angle that is when it turns around. So when facing right: 75 degrees and when facing left it knows that the angle is now 24 degrees (or whatever it would be).

    Sorry if that was confusing, I have compiled a test for you guys to show you what i am talking about. (Angle wise)
    http://www.incursiogames.com/test/
    Controls:
    up/down arrows - change angle
    left/right arrows - move

    And my code for the needle:
    Code (csharp):
    1. #pragma strict
    2.  
    3. var TopAngle: float = 0; // Max needle angle
    4. var BottomAngle: float = 0; // Min needle angle
    5.  
    6. function Start () {
    7.  
    8. }
    9.  
    10. function Update () {
    11.  
    12. // Needle movement
    13. if(Input.GetKey(KeyCode.UpArrow)) {
    14.     transform.localEulerAngles.z -= 1;
    15. }
    16.  
    17. if(Input.GetKey(KeyCode.DownArrow)) {
    18.     transform.localEulerAngles.z += 1;
    19. }
    20. // End needle movement code
    21.  
    22.  
    23. // Keep needle from moving past avaliable angles   
    24. if(transform.localEulerAngles.z >= BottomAngle  transform.localEulerAngles.z < TopAngle){  
    25.     if(transform.localEulerAngles.z < 180){  
    26.         transform.localEulerAngles.z = (BottomAngle - .1);  
    27.     } else{  
    28.         transform.localEulerAngles.z = (TopAngle + .1);  
    29.     }  
    30.  }
    31. // End needle lock code
    32.  
    33. } // End update
    34.  
     
    Last edited: May 20, 2014
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    wouldn't that just be the "eulerAngles" property of the transform, i.e. the orientation in world space as opposed to the local space you are working with in the code above?
     
  3. ubberkid

    ubberkid

    Joined:
    Mar 11, 2014
    Posts:
    17
    Ill give it a try, thanks! Sorry, i am pretty new to unity and didn't know the difference between local and world. Ill look into the documentation a bit more :)