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 caiodmk · Mar 15, 2012 at 03:17 AM · cameralerpswitch

MouseOrbit Camera Lerp

I'm making a game where you control 2 characters, when u press "E", it switch between them. For my camera I'm using unity's MouseOrbit script. When I switch the character been controlled I change the focus of my camera, but it jumps to the new position. How can I make it smoothly "lerp" to that position? Codes would help, I'm not used to java, I usually use C#.

That's the code as it is now:

 var target : Transform;
 var distance = 10.0;
 
 var xSpeed = 250.0;
 var ySpeed = 120.0;
 
 var yMinLimit = -20;
 var yMaxLimit = 80;
 
 private var x = 0.0;
 private var y = 0.0;
 
 var player1 : Transform;
 var player2 : Transform;
 
 var activePlayer : Transform;
 
 
 @script AddComponentMenu("Camera-Control/Mouse Orbit")
 
 function Start () {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
 
     target = player1;
     
     // Make the rigid body not change rotation
        if (rigidbody)
         rigidbody.freezeRotation = true;
 }
 
 function Update(){
     SwitchFocus();
 }
 
 function SwitchFocus(){
     if (Input.GetKeyDown(KeyCode.E)){    
     Debug.Log("oi");        
         if(activePlayer == player2){
             activePlayer = player1;
         }else{
             activePlayer = player2;
         }
         SetActivePlayer();
     }
 }
 
 function SetActivePlayer(){
     target = activePlayer;
 }
 
 
 function LateUpdate () {
     if (target) {
         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
          
          y = ClampAngle(y, yMinLimit, yMaxLimit);
                 
         var rotation = Quaternion.Euler(y, x, 0);
         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
         
         transform.rotation = rotation;
         transform.position = position;
     }
 }
 
 static function ClampAngle (angle : float, min : float, max : float) {
     if (angle < -360)
         angle += 360;
     if (angle > 360)
         angle -= 360;
     return Mathf.Clamp (angle, min, max);
 }
Comment
Add comment · Show 3
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 syclamoth · Mar 15, 2012 at 03:22 AM 0
Share

This isn't java, it's JavaScript (it's a very different thing, see) but I'll let that slide. I could give you a C# translation of this script, if it'd help?

avatar image caiodmk · Mar 15, 2012 at 03:28 AM 0
Share

I actually meant JavaScript, and yes it would help, so I can work only with C# scripts. But I still need to find a way to move the camera, or maybe a way to store the 2 positions the camera would have in a variable, so I can lerp between them.

avatar image syclamoth · Mar 15, 2012 at 04:05 AM 0
Share

I'm feeling generous.

 public Transform target;
 public float distance = 10.0f;

 public float xSpeed = 250.0f;
 public float ySpeed = 120.0f;

 public float y$$anonymous$$inLimit = -20f;
 public float y$$anonymous$$axLimit = 80f;

 float x = 0.0f;
 float var y = 0.0f;

 public Transform player1;
 public Transform player2;

 private Transform currentPlayer;

 // I removed the $$anonymous$$enu thing, because it goes outside the class...
 void Start () {
     Vector3 angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;

     target = player1;

     // $$anonymous$$ake the rigid body not change rotation
     if (rigidbody)
        rigidbody.freezeRotation = true;
 }

 void Update(){
     SwitchFocus();
 }

 void SwitchFocus(){
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)){  
        Debug.Log("oi");     
        if(activePlayer == player2){
            activePlayer = player1;
        }else{
            activePlayer = player2;
        }
         SetActivePlayer();
     }
 }

 void SetActivePlayer(){
     target = activePlayer;
 }


 void LateUpdate () {
     if (target) {
         x += Input.GetAxis("$$anonymous$$ouse X") * xSpeed * 0.02f;
         y -= Input.GetAxis("$$anonymous$$ouse Y") * ySpeed * 0.02f;

         y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);

         Quaternion rotation = Quaternion.Euler(y, x, 0);
         Vector3 position = rotation * Vector3(0.0f, 0.0f, -distance) + target.position;

         transform.rotation = rotation;
         transform.position = position;
     }
 }

 static float ClampAngle (float angle, float $$anonymous$$, float max) {
     if (angle < -360)
        angle += 360;
     if (angle > 360)
        angle -= 360;
     return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max);
 }

And now you don't have to use JavaScript at all.

1 Reply

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

Answer by syclamoth · Mar 15, 2012 at 03:31 AM

How about this: remove any modifications you've done to this script, and go about it from a different angle.

What is it, exactly, that you want to achieve? You want the target of the mouseOrbit to smoothly translate to the position of the new character. So, why modify the orbit script at all?

All you need to do is set the orbit script to follow an empty transform, and move that around in a smooth fashion. After all, the mouse orbit doesn't need to know about how its target is being moved, so splitting the scripts up makes sense.

On this new object, add a script that works something like this:

 public Transform initialTarget;
 Transform currentTarget;
 Transform nextTarget;

 void Start()
 {
     currentTarget = initialTarget;
     transform.parent = currentTarget;
 }

 public void MoveToNewTarget(Transform newTarget, float time)
 {
     transform.parent = null;
     nextTarget = newTarget;
     StartCoroutine(MoveCoroutine(time));
 }

 IEnumerator MoveCoroutine(float timeToMove)
 {
     float currentTime = 0;
     while(currentTime < timeToMove)
     {
         float currentDistance = currentTime / timeToMove;
         transform.position = Vector3.Lerp(currentTarget.position, nextTarget.position, currentDistance)
         transform.rotation = Quaternion.Slerp(currentTarget.rotation, nextTarget.rotation, currentDistance);
         currentTime += Time.deltaTime;
         yield return null;
     }
     currentTarget = nextTarget;
     nextTarget = null;
     transform.position = currentTarget.position;
     transform.rotation = currentTarget.rotation;
     transform.parent = currentTarget;
 }

Whenever you want to switch to a new target, just call MovableTarget.MoveToNewTarget(nextTransform, 5); to move to transform 'nextTranform' ovef 5 seconds.

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 caiodmk · Mar 15, 2012 at 03:45 AM 0
Share

I'm trying to make it work, will get back with feedback soon.

Edit:

Thanks alot, it worked just as i wanted it to.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Single Camera Lerping between three characters? (code provided) 0 Answers

character switching on collision 1 Answer

Lerping Field Of View is buggy 1 Answer

[QuaternionSlerp] Lookback script 1 Answer

Zooming camera using move z position. 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