0
\$\begingroup\$

I have some trouble to understand, and handle Quaternion and Vector3. I'm improving the tutorial Tank of Unity:

EDIT: Because a video explain so much better, there is a link to the 30 secondes video I made: https://www.youtube.com/watch?v=nNrmlfbHNTI&feature=youtu.be

In the first part, the turret turn with restriction made with the both line newRotation.z = 0.0f; and newRotation.x = 0.0f; (see code bellow).

In the second part, I just comment this two line.

My probleme is: i want to be able to look up and down (like in the second part of the video, but with a DEPRESSION LIMIT (don't look too much high and too much down)

I try soo manny case and can't manage the thing i want to do... there is the code of the script in ref to the Tank turret:

public float m_TurnSpeed = 8f;
private Transform m_Transform;
private int floorMask;
private float camRayLength = 100f;

void Awake()
{
    floorMask = LayerMask.GetMask ("Ground");
    m_Transform = GetComponent <Transform> ();
}

void FixedUpdate()
{
    Turning ();
}

void Turning()
{
    Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    RaycastHit floorHit;

    if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
    {
        Vector3 playerToMouse = floorHit.point - transform.position;
        Quaternion newRotation = Quaternion.Slerp(m_Transform.rotation, Quaternion.LookRotation(playerToMouse), Time.deltaTime * m_TurnSpeed);

/////////////////////////////////////////////////////////////////////
////////// EDIT 2: probleme solved with the following line //////////
/////////////////////////////////////////////////////////////////////

        //block totaly Z and X quanta
        //newRotation.z = 0.0f; 
        //newRotation.x = 0.0f;

        m_Transform.rotation = newRotation;

        float tmpX = m_Transform.localEulerAngles.x;
        if (tmpX > 20.0f && tmpX < 90.0f)
            tmpX = 20.0f;
        else if (tmpX < 315.0f && tmpX > 280.0f)
            tmpX = 315.0f;
        m_Transform.localEulerAngles = new Vector3(tmpX , m_Transform.localEulerAngles.y, 0.0f);
    }
}

Thanks !

\$\endgroup\$
  • \$\begingroup\$ Make a video of the problem, put it on youtube, link it here. Easier to understand. \$\endgroup\$ – Engineer Apr 6 '16 at 12:51
1
\$\begingroup\$

Looks like you need to restrict the euler angles of the turret, after the quaternion rotation has been effected. basically you'd just do something like:

transform.localEulerAngles = new Vector3(transform.localEulerAngles.x < 0 ? 0 : transform.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z);

...I think X is the right axis and not sure if it should be < or > and I think 0 rather than 90, 180 or 270. Try variations and you should get results.

\$\endgroup\$
  • \$\begingroup\$ It was the good function to look at, after some research, i found the way to sole it, thanks you ! I edit my function. \$\endgroup\$ – Ugo Hed Apr 6 '16 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.