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;
}
}
}
}
}