• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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 alt text

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.

alt text

normal.png (95.4 kB)
pinched.png (91.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Rotation from AngleAxis() ToAngleAxis() flips back and forth 1 Answer

Setting rotation moves the gameobject? 1 Answer

Multiply Quaternion by transform.Forward Results 0 Answers

Joystick movement conversion to angle doesn't work anymore in C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges