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
0
Question by KayoticSully · Oct 16, 2011 at 11:12 AM · rotation

[Problem] "Security Camera" rotation bounds - stop rotation if player goes past a specific point

Background: I am working on a Security Camera prefab. It is fairly simple so far. I have a camera model that I make look at the player if the player is within some specified distance to the camera. Otherwise it pans back and forth between two points I have set in the 3D space close to the camera.

Problem: The problem I am having is I want the camera to never go past those limits. If the player moves past it I want the camera to stay looking at the limit until the player is out of range. I will post my full code.

Any help is appreciated, I have been trying to figure out the best way to do this for a while now.

Code:

 /* Settings */
 var range = 40;
 var followSpeed : float = 1.0;
 var panSpeed : float = .5 ;
 
 /* Exposed Objects */
 var player : Transform;
 var leftLimit : Transform;
 var rightLimit : Transform;
 
 private var currentPanTarget : Transform = rightLimit;
 
 function Start(){
     // pick a random Direction
     if(Random.Range(1, 10) % 2 == 0){
         currentPanTarget = leftLimit;
     } else {
         currentPanTarget = rightLimit;
     }
 }
 
 function Update () {
     // calculate distance to player
     var currentDistance = Vector3.Distance(transform.position, player.position);
     
     /* if the player is within the range panTo player */
     if(currentDistance < range){
         panTo(player, followSpeed);
     } else {
         panBehavior();
     }
 }
 
 function panTo(target : Transform, speed : float){
     var relativePos = target.position - transform.position;
     var rotation = Quaternion.LookRotation(relativePos);
     
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * speed);
 }
 
 
 function panBehavior(){
     var relativePos = currentPanTarget.position - transform.position;
     
     var lookingAtLimit = false;
     if(Vector3.Angle(relativePos, transform.forward) < 20){
         lookingAtLimit = true;
     }
     
     // if needed swap direction
     if(lookingAtLimit){
         if(currentPanTarget == leftLimit){
             currentPanTarget = rightLimit;
         } else if(currentPanTarget == rightLimit) {
             currentPanTarget = leftLimit;
         }
     }
     
     panTo(currentPanTarget, panSpeed);
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Proportion · Oct 16, 2011 at 12:00 PM

ey what you want is transform.eulerAngle.y (assuming your camera is rotating along the y axis). if you put that in a public variable, that will show what angle your camera is pointing (from 0 to 360 degrees). then use what eulerangles u want as the maximum range.

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 KayoticSully · Oct 17, 2011 at 07:53 AM 0
Share

I tried implementing that in multiple ways and it does not seem to be working at all. Could you be a little more clear on how to implement it in the script? I am fairly new to euler angles and Quaternions. It might also help to know that the camera rotates in 3 dimensions. So it follows the player, left, right, and up and down. I have been banging my head off a wall trying to figure this out. It might be that I do not have enough experience with these math constructs to know what operations I actually need to detect if the player has gone outside the bounds.

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

Object Rotation doesnt work 0 Answers

Clamp Rotation Problem 1 Answer

Problem changing rotation of 3rd Person Controller 1 Answer

Rotation Constraint doesn't work properly 1 Answer

2 axis rotated towards 2 different points 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