Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by redteardrop · Aug 10, 2012 at 11:54 AM · rotationanglelookatclampturret

How to clamp Lookat?

I have an object following another. As an example a security camera shouldn't be able to do a full 360 if the wall is in the way or a turret should be limited so it won't go past a certain angle and shoot it's team mates. I was hoping I could use something like this:

transform.LookAt(target);
if(transform.rotation.x > 60){transform.rotation.x = 60;}
I guess the best way to do this would be to use a euler rotation derived from the target
someAngle = Mathf.Clamp(someAngle, MinClamp, MaxClamp);
transform.rotation = Quaternion.Euler(someAngle,0,0);
I don't know how to get the someAngle from the target/origin positions which is why I was using lookAt since that's what it's for. Any help clamping the angle on one axis?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by redteardrop · Aug 11, 2012 at 08:08 AM

I figured out how to do it and am posting in case anyone else needs it.
You'll need 3 things. 1. a target 2. the object you want to do the looking 3. a dummy object (just make sure your dummy is a child of your object so it'll share the parent rotation and position or you'll get some odd angles)
Set the dummy to look at the target, read the dummy rotation into a variable then clamp that variable and pass it onto the rotaion of the final object.

var dummyObject:Transform; var targetPoint:Transform; var tempAngle:float; function LateUpdate() { dummyObject.LookAt(targetPoint); if(Cannon3.transform.rotation.x != 0) //seem to get a 0 if my target is in //certain places because I am using a mouse pos and hit with plane. { tempAngle = Cannon3.rotation.x * 100; //don't know how to read a euler //angle into a float so I just multiplied by 100 to get the angle. } else { tempAngle = tempAngle; }

 Cannon1.rotation = Quaternion.Euler(tempAngle,0,0);

}

Comment
Add comment · Show 1 · Share
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
avatar image astracat111 · Aug 14, 2017 at 06:43 AM 0
Share

Great solution. And remember everyone you can dynamically create game objects, just do like GameObject dummyObject = new GameObject(); dummyObject.name = "Dummy Object"; dummyObject.transform.parent.transform = this.gameObject.transform;

avatar image
0

Answer by ScroodgeM · Aug 10, 2012 at 09:09 PM

sol 1

declare/create a basic quaternion that will be central rotation of object's 'white (where object can rotate only)' or 'black(where object can't rotate)' area on rotation calc look rotation and calc an angle between calculated and one from previous tick * if angle more then maximum (for white) or lower then minimum (for black) - just move away from black or toward white for an angle then was over limit

sol 2

use vector3 projections to different axis to calc offsets and limit them if needed. for example, Vector3.Dot(....) returns cosine of angle between vectors muliplied with it's distances. so if you got negative value it means angle between them is more them 90 degrees. all you need now is declare some vectors that will used for this limits.

creating some workflow visual gizmos will be perfect in this case

Comment
Add comment · Show 3 · Share
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
avatar image redteardrop · Aug 11, 2012 at 05:46 AM 2
Share

That may be understandable to some, but I haven't a clue to what you're talking about. I figured out a way to get it done. I think it's a combination of what you were talking about and my own solution.

avatar image ScroodgeM · Aug 11, 2012 at 08:07 AM 1
Share

i can explain if you point to place that is understandable.

avatar image redteardrop · Aug 11, 2012 at 12:50 PM 1
Share

I would probably ask for some coding example, but since I found a way to get it to work exactly as I need it I think I'll leave it at that. Thanks though. :-)

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make object only rotate in 90 degree increments 0 Answers

Constraining turret rotation in local space 1 Answer

How to clamp the rotation of this? 0 Answers

problems with quaternion rotations 4 Answers

How do I limit the vertical rotation of a camera? 0 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