Search Unity

How to make your object stay at 45 degree angle?

Discussion in 'Scripting' started by hizral, Nov 10, 2014.

  1. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    Hello there,

    Have a bit of problem here, tyring to make my object rotate in its own space..Tried using the
    Code (CSharp):
    1. transform.Rotate (0, Time.deltaTime * rotationSmooth, 0, Space.Self);
    it work okay no problem at all, but this rotation is contast and will not stop.

    so I tried used this code below so I can control the rotation of my object. But the problem is "tranform.rotation" you cannot used the "Space.Self" in the code. So is there any other way that I can used "transform.rotation" with "Space.Self"
    Code (CSharp):
    1. target = Quaternion.Euler (0, 90f, 0);
    2. transform.rotation = Quaternion.Slerp (upin.gameObject.transform.rotation, target, Time.deltaTime * rotationSmooth);
    thank you.
     
  2. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Code (csharp):
    1. Transform.localRotation
    ?
     
  3. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    Hello sluice, I dont think I can used "Transform.localRotation". the result will be the same i think, What I want is I want my object rotate on it own space. for example. My object is rotate around 45 degree angle and I would like it to rotare on its Y axis but still maintaion the 45 degree angle.
     
  4. SaraCecilia

    SaraCecilia

    Joined:
    Jul 9, 2014
    Posts:
    675
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Code (CSharp):
    1. transform.eulerAngles += new Vector3(x, y, z);
    What do you mean with rotate it in self space?
    Maybe this might help you o.o
     
  6. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    Hello everyone, sorry for the really late reply,

    Okay as you guys suggested using
    Code (CSharp):
    1. Transform.localRotation
    2.  
    It does work, but having a bit of other problem here, How do I make my object stay at 45 degree angle. Because the object will return back to 90 degree angle if i play start, after I press button that change the
    Code (CSharp):
    1. targetUpin = Quaternion.Euler (0, 180f, 0);
    , than will the object return back to 45 degree angle.

    How do i fix this.

    Below here is my full script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour {
    5.  
    6.     public GameObject upin;
    7.     public GameObject ipin;
    8.     public float rotationSmooth;
    9.     Quaternion targetUpin;
    10.     Quaternion targetIpin;
    11.     Vector3 rotUpin;
    12.     Vector3 rotIpin;
    13.     public float roundYUpin;
    14.     public float roundYIpin;
    15.  
    16.     public Transform [] touchControl;
    17.  
    18.     public Camera touchCamera;
    19.     public bool touchAllowed;
    20.     Touch touch;
    21.     int id;
    22.     Ray ray;
    23.     RaycastHit hit;
    24.  
    25.     public Transform [] gunMuzzle;
    26.     public Transform bullet;
    27.     public bool shoot;
    28.     public bool fireAllowed;
    29.  
    30.     public bool lookNow;
    31.     public float lookFront;
    32.  
    33.     // Use this for initialization
    34.     void Start ()
    35.     {
    36.         touchAllowed = true;
    37.         fireAllowed = true;
    38.         lookNow = false;
    39.     }
    40.    
    41.     // Update is called once per frame
    42.     void Update ()
    43.     {
    44.         upin.gameObject.transform.rotation = Quaternion.Slerp (upin.gameObject.transform.rotation , Quaternion.Euler (45, 0, 0) * targetUpin, Time.deltaTime * rotationSmooth);
    45.         ipin.gameObject.transform.rotation = Quaternion.Slerp (ipin.gameObject.transform.rotation , Quaternion.Euler (45, 0, 0) * targetIpin, Time.deltaTime * rotationSmooth);
    46.  
    47.         rotUpin = upin.gameObject.transform.eulerAngles;
    48.         roundYUpin = rotUpin.y;
    49.         roundYUpin = (float) System.Math.Round (roundYUpin, 2);
    50.  
    51.         rotIpin = ipin.gameObject.transform.eulerAngles;
    52.         roundYIpin = rotIpin.y;
    53.         roundYIpin = (float) System.Math.Round (roundYIpin, 2);
    54.  
    55.         if (touchAllowed)
    56.         {
    57.             ray = touchCamera.ScreenPointToRay (Input.mousePosition);
    58.  
    59.             if (Input.GetMouseButtonDown (0))
    60.             {
    61.                 lookFront = 0.0f;
    62.                 lookNow = true;
    63.                 shoot = false;
    64.                                                            
    65.                 if (Physics.Raycast (ray, out hit, 100))
    66.                 {
    67.                     if (hit.collider.tag == "touchUp")
    68.                     {
    69.                         targetUpin = Quaternion.Euler (0, 180f, 0);
    70.  
    71.                         if (!shoot)
    72.                         {
    73.                             Instantiate (bullet, gunMuzzle [0].position, gunMuzzle [0].rotation);
    74.                             bullet.rotation = gunMuzzle [0].rotation;
    75.                             shoot = true;
    76.                         }
    77.                     }
    78.                     else if (hit.collider.tag == "touchLeft")
    79.                     {
    80.                         targetUpin = Quaternion.Euler (0, 90f, 0);
    81.  
    82.                         if (!shoot)
    83.                         {
    84.                             Instantiate (bullet, gunMuzzle [1].position, gunMuzzle [1].rotation);
    85.                             bullet.rotation = gunMuzzle [1].rotation;
    86.                             shoot = true;
    87.                         }
    88.                     }
    89.                     else if (hit.collider.tag == "touchUpLeft")
    90.                     {
    91.                         targetUpin = Quaternion.Euler (0, 138, 0);
    92.  
    93.                         if (!shoot)
    94.                         {
    95.                             Instantiate (bullet, gunMuzzle [2].position, gunMuzzle [2].rotation);
    96.                             bullet.rotation = gunMuzzle [2].rotation;
    97.                             shoot = true;
    98.                         }
    99.                     }
    100.                     else if (hit.collider.tag == "touchDownLeft")
    101.                     {
    102.                         targetUpin = Quaternion.Euler (0, 55f, 0);
    103.  
    104.                         if (!shoot)
    105.                         {
    106.                             Instantiate (bullet, gunMuzzle [3].position, gunMuzzle [3].rotation);
    107.                             bullet.rotation = gunMuzzle [3].rotation;
    108.                             shoot = true;
    109.                         }
    110.                     }
    111.  
    112.                     if (hit.collider.tag == "touchDown")
    113.                     {
    114.                         targetIpin = Quaternion.Euler (0, 0, 0);
    115.  
    116.                         if (!shoot)
    117.                         {
    118.                             Instantiate (bullet, gunMuzzle [4].position, gunMuzzle [4].rotation);
    119.                             bullet.rotation = gunMuzzle [4].rotation;
    120.                             shoot = true;
    121.                         }
    122.                     }
    123.                     else if (hit.collider.tag == "touchRight")
    124.                     {
    125.                         targetIpin = Quaternion.Euler (0, -90f, 0);
    126.  
    127.                         if (!shoot)
    128.                         {
    129.                             Instantiate (bullet, gunMuzzle [5].position, gunMuzzle [5].rotation);
    130.                             bullet.rotation = gunMuzzle [5].rotation;
    131.                             shoot = true;
    132.                         }
    133.                     }
    134.                     else if (hit.collider.tag == "touchUpRight")
    135.                     {
    136.                         targetIpin = Quaternion.Euler (0, -138f, 0);
    137.  
    138.                         if (!shoot)
    139.                         {
    140.                             Instantiate (bullet, gunMuzzle [6].position, gunMuzzle [6].rotation);
    141.                             bullet.rotation = gunMuzzle [6].rotation;
    142.                             shoot = true;
    143.                         }
    144.                     }
    145.                     else if (hit.collider.tag == "touchDownRight")
    146.                     {
    147.                         targetIpin = Quaternion.Euler (0, -55f, 0);
    148.  
    149.                         if (!shoot)
    150.                         {
    151.                             Instantiate (bullet, gunMuzzle [7].position, gunMuzzle [7].rotation);
    152.                             bullet.rotation = gunMuzzle [7].rotation;
    153.                             shoot = true;
    154.                         }
    155.                     }
    156.                 }
    157.             }
    158.  
    159.             if (lookNow)
    160.             {
    161.                 lookFront += Time.deltaTime;
    162.  
    163.                 if (rotUpin.y != 0f && lookFront > 5f)
    164.                 {
    165.                     targetUpin = Quaternion.Euler (0, 0, 0);
    166.                     lookFront = 0.0f;
    167.                     lookNow = false;
    168.                 }
    169.             }
    170.         }
    171.     }
    172. }
    173.  
    174.  
    175.  
     
  7. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    Bump, Anyone can help me here.