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 Spincu · Jul 24, 2013 at 01:29 PM · camerarotationmouseturretaim

Freeze Camera Rotation that is attached to the object in certain moments

Ok so this is the code I have so far:

 //public variables
 public var Target      : Transform;   //object to follow
 public var SwivelSpeed : float = 1.0; 
 
 public var Boundary : int = 70; // distance from edge scrolling starts
 public var speed : int = 5;
  
 //private variables
 private var theScreenWidth : int;
 private var theScreenHeight : int;
 
 
 // start function
 function Start(){
    Screen.showCursor = false;
    theScreenWidth = Screen.width;
    
 }
 
 // update function 
 function Update () 
 { 
  RotateTurret();
 }
 
 // rotate function
 function RotateTurret(){
 
 if (Input.mousePosition.x > theScreenWidth - Boundary)
     {
         if(Target)
         {
            
         localTarget=   transform.InverseTransformDirection(Target.transform.position - transform.position);
         targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
         adjustedAngle = Mathf.Lerp(0, targetAngle, Time.deltaTime * SwivelSpeed);
         
             transform.Rotate(Vector3.up, adjustedAngle);
         }   
     }
  
 else if (Input.mousePosition.x < 0 + Boundary)
     {
        if(Target)
         {
            
         localTarget=   transform.InverseTransformDirection(Target.transform.position - transform.position);
         targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
         adjustedAngle = Mathf.Lerp(0, targetAngle, Time.deltaTime * SwivelSpeed);
         
             transform.Rotate(Vector3.up, adjustedAngle);
         }   
     }
     
 else 
     {      
         //// ? freeze camera 
         
         if(Target)
         {
            
         localTarget=   transform.InverseTransformDirection(Target.transform.position - transform.position);
         targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
         adjustedAngle = Mathf.Lerp(0, targetAngle, Time.deltaTime * SwivelSpeed);
         
             transform.Rotate(Vector3.up, adjustedAngle);
         }   
     }
 }

It rotates a turret to look at an objects location. All is good when camera is not attached to the turret. But when I attach the camera to the turret ( because I need the player to be looking where the turret is pointing at .. ) It spins continuosly because the objects is raycasted with the mouse on screen. And the turret spins towards the object , but because the camera is on the turret and the turret spins, the object is never in the same place thus the turret never stops spinning.

So I made a condition to rotate the turret only when on the edges of the screen, but I need to put something in that "else statement" that lets the turret follow the object but the camera freezes or detaches itself from the turret rotation somehow.

Other solutions I've tried so far: Two cameras, on the body and on the turret and disabling them on certain movements but there it's not fluid as it jumps from one camera to the other, comparing mouse positions to previous one but that didn't work either, detaching the camera from the turret and making it follow it with a script, but I can't seem to write a corect script that would rotate around the turret at the same pace with it and around it so I abandoned that ideea.

Basically I need the camera to stop following the turret in that "else" but I stiil need to use this if(Target) { localTarget= transform.InverseTransformDirection(Target.transform.position - transform.position); targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg; adjustedAngle = Mathf.Lerp(0, targetAngle, Time.deltaTime * SwivelSpeed); transform.Rotate(Vector3.up, adjustedAngle); } to rotate my turret.

Can you please help ? I'm really amazed there is not a freeze that can oveeride a camera rotating with it's parent.

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

15 People are following this question.

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

Related Questions

Rotation always changing values! 1 Answer

How to make an object rotate on the y axis towards the mouse? 3 Answers

Mouse Orbit snapping issues 0 Answers

Screen.lockCursor messes my rotation 1 Answer

"Perfect" Free Rotation Method? (like Blender) 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