How to make your object stay at 45 degree angle?

Hello there,

Have a bit of problem here, tyring to make my object rotate in its own space…Tried using the 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”

target = Quaternion.Euler (0, 90f, 0);
transform.rotation = Quaternion.Slerp (upin.gameObject.transform.rotation, target, Time.deltaTime * rotationSmooth);

thank you.

Transform.localRotation ?

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.

Are you sure you cannot use localRotation? Are you trying to do something similar to this user? Euler angle rotation in self space problem. - Questions & Answers - Unity Discussions

transform.eulerAngles += new Vector3(x, y, z);

What do you mean with rotate it in self space?
Maybe this might help you o.o

Hello everyone, sorry for the really late reply,

Okay as you guys suggested using

Transform.localRotation

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 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.

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public GameObject upin;
    public GameObject ipin;
    public float rotationSmooth;
    Quaternion targetUpin;
    Quaternion targetIpin;
    Vector3 rotUpin;
    Vector3 rotIpin;
    public float roundYUpin;
    public float roundYIpin;

    public Transform [] touchControl;

    public Camera touchCamera;
    public bool touchAllowed;
    Touch touch;
    int id;
    Ray ray;
    RaycastHit hit;

    public Transform [] gunMuzzle;
    public Transform bullet;
    public bool shoot;
    public bool fireAllowed;

    public bool lookNow;
    public float lookFront;

    // Use this for initialization
    void Start ()
    {
        touchAllowed = true;
        fireAllowed = true;
        lookNow = false;
    }
   
    // Update is called once per frame
    void Update ()
    {
        upin.gameObject.transform.rotation = Quaternion.Slerp (upin.gameObject.transform.rotation , Quaternion.Euler (45, 0, 0) * targetUpin, Time.deltaTime * rotationSmooth);
        ipin.gameObject.transform.rotation = Quaternion.Slerp (ipin.gameObject.transform.rotation , Quaternion.Euler (45, 0, 0) * targetIpin, Time.deltaTime * rotationSmooth);

        rotUpin = upin.gameObject.transform.eulerAngles;
        roundYUpin = rotUpin.y;
        roundYUpin = (float) System.Math.Round (roundYUpin, 2);

        rotIpin = ipin.gameObject.transform.eulerAngles;
        roundYIpin = rotIpin.y;
        roundYIpin = (float) System.Math.Round (roundYIpin, 2);

        if (touchAllowed)
        {
            ray = touchCamera.ScreenPointToRay (Input.mousePosition);

            if (Input.GetMouseButtonDown (0))
            {
                lookFront = 0.0f;
                lookNow = true;
                shoot = false;
                                                           
                if (Physics.Raycast (ray, out hit, 100))
                {
                    if (hit.collider.tag == "touchUp")
                    {
                        targetUpin = Quaternion.Euler (0, 180f, 0);

                        if (!shoot)
                        {
                            Instantiate (bullet, gunMuzzle [0].position, gunMuzzle [0].rotation);
                            bullet.rotation = gunMuzzle [0].rotation;
                            shoot = true;
                        }
                    }
                    else if (hit.collider.tag == "touchLeft")
                    {
                        targetUpin = Quaternion.Euler (0, 90f, 0);

                        if (!shoot)
                        {
                            Instantiate (bullet, gunMuzzle [1].position, gunMuzzle [1].rotation);
                            bullet.rotation = gunMuzzle [1].rotation;
                            shoot = true;
                        }
                    }
                    else if (hit.collider.tag == "touchUpLeft")
                    {
                        targetUpin = Quaternion.Euler (0, 138, 0);

                        if (!shoot)
                        {
                            Instantiate (bullet, gunMuzzle [2].position, gunMuzzle [2].rotation);
                            bullet.rotation = gunMuzzle [2].rotation;
                            shoot = true;
                        }
                    }
                    else if (hit.collider.tag == "touchDownLeft")
                    {
                        targetUpin = Quaternion.Euler (0, 55f, 0);

                        if (!shoot)
                        {
                            Instantiate (bullet, gunMuzzle [3].position, gunMuzzle [3].rotation);
                            bullet.rotation = gunMuzzle [3].rotation;
                            shoot = true;
                        }
                    }

                    if (hit.collider.tag == "touchDown")
                    {
                        targetIpin = Quaternion.Euler (0, 0, 0);

                        if (!shoot)
                        {
                            Instantiate (bullet, gunMuzzle [4].position, gunMuzzle [4].rotation);
                            bullet.rotation = gunMuzzle [4].rotation;
                            shoot = true;
                        }
                    }
                    else if (hit.collider.tag == "touchRight")
                    {
                        targetIpin = Quaternion.Euler (0, -90f, 0);

                        if (!shoot)
                        {
                            Instantiate (bullet, gunMuzzle [5].position, gunMuzzle [5].rotation);
                            bullet.rotation = gunMuzzle [5].rotation;
                            shoot = true;
                        }
                    }
                    else if (hit.collider.tag == "touchUpRight")
                    {
                        targetIpin = Quaternion.Euler (0, -138f, 0);

                        if (!shoot)
                        {
                            Instantiate (bullet, gunMuzzle [6].position, gunMuzzle [6].rotation);
                            bullet.rotation = gunMuzzle [6].rotation;
                            shoot = true;
                        }
                    }
                    else if (hit.collider.tag == "touchDownRight")
                    {
                        targetIpin = Quaternion.Euler (0, -55f, 0);

                        if (!shoot)
                        {
                            Instantiate (bullet, gunMuzzle [7].position, gunMuzzle [7].rotation);
                            bullet.rotation = gunMuzzle [7].rotation;
                            shoot = true;
                        }
                    }
                }
            }

            if (lookNow)
            {
                lookFront += Time.deltaTime;

                if (rotUpin.y != 0f && lookFront > 5f)
                {
                    targetUpin = Quaternion.Euler (0, 0, 0);
                    lookFront = 0.0f;
                    lookNow = false;
                }
            }
        }
    }
}

Bump, Anyone can help me here.