- Home /
Question by
Craftomega · Mar 07, 2016 at 01:39 AM ·
c#rotationquaterniondebugdrawline
OnDrawGizmos Debug.DrawLine position is pinching when rotated.
I have no idea how to explain this question so this title is a mess. I am making a clamp, and I am trying to get the debug to show the max angle a object can rotate.
using UnityEngine;
using System.Collections;
public class TurretClamp : MonoBehaviour
{
public GameObject targetObject;
public GameObject parentObject;
[Header("Max - Min")]
public bool xActive = true;
public Vector2 xRotation = new Vector2(0, 0);
public bool yActive = true;
public Vector2 yRotation = new Vector2(0, 0);
public bool zActive = true;
public Vector2 zRotation = new Vector2(0, 0);
[Header("Debug")]
public bool debugActive = true;
public float distance = 1;
void Start()
{
if (!targetObject)
{
targetObject = this.gameObject;
}
if (!parentObject && targetObject.transform.parent)
{
Debug.LogError("Target Object should have a parent for the dubug");
parentObject = targetObject.transform.parent.gameObject;
}
}
// Update is called once per frame
void Update ()
{
ClampAngle(xRotation, yRotation, zRotation);
}
internal void ClampAngle(Vector2 _xRotation, Vector2 _yRotation, Vector2 _zRotation)
{
//X Axis
if (xActive)
{
if (targetObject.transform.localEulerAngles.x > _xRotation.x && targetObject.transform.localEulerAngles.x < 180)
{
targetObject.transform.localEulerAngles = new Vector3(_xRotation.x, targetObject.transform.localEulerAngles.y, targetObject.transform.localEulerAngles.z);
}
else if (targetObject.transform.localEulerAngles.x < 360 + _xRotation.y && targetObject.transform.localEulerAngles.x > 180)
{
targetObject.transform.localEulerAngles = new Vector3(_xRotation.y, targetObject.transform.localEulerAngles.y, targetObject.transform.localEulerAngles.z);
}
}
//Y Axis
if (yActive)
{
if (targetObject.transform.localEulerAngles.y > _yRotation.x && targetObject.transform.localEulerAngles.y < 180)
{
targetObject.transform.localEulerAngles = new Vector3(targetObject.transform.localEulerAngles.x, _yRotation.x, targetObject.transform.localEulerAngles.z);
}
else if (targetObject.transform.localEulerAngles.y < 360 + _yRotation.y && targetObject.transform.localEulerAngles.y > 180)
{
targetObject.transform.localEulerAngles = new Vector3(targetObject.transform.localEulerAngles.x, _yRotation.y, targetObject.transform.localEulerAngles.z);
}
}
//Z Axis
if (zActive)
{
if (targetObject.transform.localEulerAngles.z > _zRotation.x && targetObject.transform.localEulerAngles.z < 180)
{
targetObject.transform.localEulerAngles = new Vector3(targetObject.transform.localEulerAngles.x, targetObject.transform.localEulerAngles.y, _zRotation.x);
}
else if (targetObject.transform.localEulerAngles.z < 360 + _zRotation.y && targetObject.transform.localEulerAngles.z > 180)
{
targetObject.transform.localEulerAngles = new Vector3(targetObject.transform.localEulerAngles.x, targetObject.transform.localEulerAngles.y, _zRotation.y);
}
}
}
internal void OnDrawGizmos()
{
if (parentObject)
{
if (debugActive)
{
//X Rotations
if (xActive)
{
Vector3 xMax = (Quaternion.Euler(parentObject.transform.eulerAngles.x + xRotation.x, 0, 0) * parentObject.transform.forward * distance) + transform.position;
Vector3 xMin = (Quaternion.Euler(parentObject.transform.eulerAngles.x + xRotation.y, 0, 0) * parentObject.transform.forward * distance) + transform.position;
Vector3 direction = (Quaternion.Euler(targetObject.transform.eulerAngles.x, 0, 0) * parentObject.transform.forward * distance) + transform.position;
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(direction, 0.03f);
//Debug.DrawLine(targetObject.transform.position, direction, Color.red);
Debug.DrawLine(targetObject.transform.position, xMax, Color.red);
Debug.DrawLine(targetObject.transform.position, xMin, Color.red);
}
//Y Rotations
if (yActive)
{
Vector3 yMax = (Quaternion.Euler(0, parentObject.transform.eulerAngles.y + yRotation.x, 0) * parentObject.transform.forward * distance) + transform.position;
Vector3 yMin = (Quaternion.Euler(0, parentObject.transform.eulerAngles.y + yRotation.y, 0) * parentObject.transform.forward * distance) + transform.position;
Vector3 direction = (Quaternion.Euler(0, targetObject.transform.eulerAngles.y, 0) * parentObject.transform.forward * distance) + transform.position;
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(direction, 0.03f);
//Debug.DrawLine(targetObject.transform.position, direction, Color.green);
Debug.DrawLine(targetObject.transform.position, yMax, Color.green);
Debug.DrawLine(targetObject.transform.position, yMin, Color.green);
}
//Z Rotations
if (zActive)
{
//float a = (Mathf.Sin(zRotation.x) * distance) / Mathf.Sin(90);
//float b = (Mathf.Sin(180 - zRotation.x - 90f) * distance) / Mathf.Sin(90);
//Vector3 av = new Vector3(a, 0, 0) + transform.right;
//Vector3 bv = new Vector3(0, b, 0) + transform.up;
//Vector3 zMax = new Vector3(bv.y, av.x, 0) + transform.position;
Vector3 zMax = (Quaternion.Euler(0, 0, parentObject.transform.eulerAngles.z + zRotation.x) * parentObject.transform.up * distance) + transform.position;
Debug.Log(zMax);
Vector3 zMin = (Quaternion.Euler(0, 0, parentObject.transform.eulerAngles.z + zRotation.y) * parentObject.transform.up * distance) + transform.position;
Vector3 direction = (Quaternion.Euler(0, 0, targetObject.transform.eulerAngles.z) * parentObject.transform.up * distance) + transform.position;
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(direction, 0.03f);
//Debug.DrawLine(targetObject.transform.position, direction, Color.blue);
Debug.DrawLine(parentObject.transform.position, zMax, Color.blue);
Debug.DrawLine(parentObject.transform.position, zMin, Color.blue);
}
}
}
else
{
if (debugActive)
{
//X Rotations
if (xActive)
{
Vector3 xMax = (Quaternion.Euler(xRotation.x, 0, 0) * Vector3.forward * distance) + transform.position;
Vector3 xMin = (Quaternion.Euler(xRotation.y, 0, 0) * Vector3.forward * distance) + transform.position;
Vector3 direction = (Quaternion.Euler(targetObject.transform.eulerAngles.x, 0, 0) * Vector3.forward * distance) + transform.position;
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(direction, 0.03f);
//Debug.DrawLine(targetObject.transform.position, direction, Color.red);
Debug.DrawLine(targetObject.transform.position, xMax, Color.red);
Debug.DrawLine(targetObject.transform.position, xMin, Color.red);
}
//Y Rotations
if (yActive)
{
Vector3 yMax = (Quaternion.Euler(0, yRotation.x, 0) * Vector3.forward * distance) + transform.position;
Vector3 yMin = (Quaternion.Euler(0, yRotation.y, 0) * Vector3.forward * distance) + transform.position;
Vector3 direction = (Quaternion.Euler(0, targetObject.transform.eulerAngles.y, 0) * Vector3.forward * distance) + transform.position;
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(direction, 0.03f);
//Debug.DrawLine(targetObject.transform.position, direction, Color.green);
Debug.DrawLine(targetObject.transform.position, yMax, Color.green);
Debug.DrawLine(targetObject.transform.position, yMin, Color.green);
}
//Z Rotations
if (zActive)
{
Vector3 zMax = (Quaternion.Euler(0, 0, zRotation.x) * Vector3.up * distance) + transform.position;
Vector3 zMin = (Quaternion.Euler(0, 0, zRotation.y) * Vector3.up * distance) + transform.position;
Vector3 direction = (Quaternion.Euler(0, 0, targetObject.transform.eulerAngles.z) * Vector3.up * distance) + transform.position;
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(direction, 0.03f);
//Debug.DrawLine(targetObject.transform.position, direction, Color.blue);
Debug.DrawLine(targetObject.transform.position, zMax, Color.blue);
Debug.DrawLine(targetObject.transform.position, zMin, Color.blue);
}
}
}
}
}
When the X is rotated the Z debugs clamp together. See picts
How its supposed to look
This is what happens when I rotate the parent. I want the debugs to stay pointing above the parent object no matter what rotation its at.
normal.png
(95.4 kB)
pinched.png
(91.0 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612070558im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
How can i rotate a object more than once (90 degrees)? 0 Answers
Camera not rotating 1 Answer
Calculating the rotation of an object, when the top is forward 1 Answer
Auto leveling help 0 Answers