Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 DwarvenGaming · Nov 08, 2016 at 07:35 AM · rotationvector3mathf.clampx-axis

Restricting rotation on an axis

Okay, So I have a turret. On this turret the base rotates around the Y axis, facing the target and the GUN the sort of barrel of the turret which is a separate game object rotates on the X axis facing the target. This works beautifully, my only issue comes in when I try to restrict the turrets rotation. If you look at the photo I included (Sorry it's so rough) I want the turret to be able to look at the target from 0-45 X so that it doesn't look into the floor and so that it never has to look straight up.

Here is my Code:

 using UnityEngine;
 using System.Collections;
 
 public class TurretTargeting : MonoBehaviour {
     GameObject turretBase;
     GameObject turretGun;
 
     public Transform target;
 
     public bool clamp;
     public float c1, c2;
 
     private float x = 0.0f;
 
     // Use this for initialization
     void Start()
     {
         turretBase = transform.FindChild("Turret_Base").gameObject;
         turretGun = turretBase.transform.FindChild("Turret_Gun").gameObject;
         x = turretGun.transform.eulerAngles.x;
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
         if (target != null)
         {
             Vector3 dir = target.transform.position - turretBase.transform.position;
             Vector3 dir1 = target.transform.position - turretGun.transform.position;
 
             Quaternion lookRot = Quaternion.LookRotation(dir);
             Quaternion lookRot1 = Quaternion.LookRotation(dir1);
 
 
             //This doesnt work at all.
             if (clamp)
             {
                 x = ClampAngle(x, -c1, c2);
                 lookRot1.x = x;
 
             }
 
             turretBase.transform.rotation = Quaternion.Euler(0, lookRot.eulerAngles.y, 0);
             turretGun.transform.localRotation = Quaternion.Euler(lookRot1.eulerAngles.x, 0, 0);
             //-----------------------
             
 
         }
         else
         {
             turretBase.transform.rotation = new Quaternion(0, 0, 0, 0);
             turretGun.transform.rotation = new Quaternion(0, 0, 0, 0);
         }
     }
 
     float ClampAngle(float angle, float min, float max)
     {
         if (angle < -360)
             angle += 360;
         if (angle > 360)
             angle -= 360;
 
         return Mathf.Clamp(angle, min, max);
     }
 }
 
 

I have been trying to wrap my head around this for hours with no results, so if anyone has any solutions please help.

Photo I Mentioned. alt text

question-photo.png (53.5 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rotating a character along the x and z-axis to align with a vector3 direction. 1 Answer

Making an object realistically rebound off walls 1 Answer

How to make a gameobject have a rotation constraint? 1 Answer

How can I replace this Vector3 with a float? 2 Answers

How do I align my player transform to waypoints and without restricting Z Axis rotation? 2 Answers


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