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 ronronmx · May 13, 2012 at 02:33 AM · cameracamera-looksmoothfollow

Smooth move + orbit camera around target

Hey guys, got a tough one here. I have 2 versions of my camera code...

One version creates a smooth camera movement with different dampening for different directions (this version doesn't turn or orbit, it just follows the target sideways).

The other version is basically the orbit camera code from the standard assets with minor mods.

What i'd like to do is take my custom smooth camera code and make it orbit around the target. Here's my custom smooth camera code:

 public void SmoothCamFollow ( Transform target, Transform camTransform, bool getRelativePos ) {
         
         if( getRelativePos )
         {
             relativeBikePos = Mathf.Abs( Mathf.Clamp( target.InverseTransformPoint( camTransform.position ).y, camMov.maxCamPosZ, 0f ) );
         }
         
         // Get current velocity of the target.
         bikeVelocity = target.rigidbody.velocity;
         
         
         // we define some temporary variables for raycasting...
         // Starting point of the ray.
         rayStart = target.transform.position + new Vector3( 0, 2, 0 );
         
         RaycastHit hit;
         
         // Cast a ray forward to check if terrain is going uphill (used for both cameras)...
         if( Physics.Raycast( rayStart, rayDirRight, out hit, camMov.rayLenghtRight, layerMask ) )
         {
             goingUpHill = true;
         }
         else
         {
             goingUpHill = false;
         }
 
         // We now calculate the offset of the camera by adding camOffset to the target's position.
         camPosX = target.position.x + camMov.camOffsetX;
         camPosY = target.position.y + camMov.camOffsetY;
         camPosZ = target.position.z + camMov.camOffsetZ;
         
         /// <summary>
         /// Create the "smooth" effect when the camera follows the bike.
         /// </summary>
         camRight    = Mathf.Lerp( camTransform.position.x, camPosX, Time.deltaTime * camMov.rightDamping );
         camLeft        = Mathf.Lerp( camTransform.position.x, camPosX, Time.deltaTime * camMov.leftDamping );
         camUp        = Mathf.Lerp( camTransform.position.y, camPosY, Time.deltaTime * camMov.upDamping );
         camDown        = Mathf.Lerp( camTransform.position.y, camPosY, Time.deltaTime * camMov.downDamping );
         camGround    = Mathf.Lerp( camTransform.position.y, camPosY, Time.deltaTime * camMov.groundDamping );
 
         /// <summary>
         /// Now let's zoom in and out
         /// </summary>
         maxCamZoomZ        = Mathf.Lerp( camTransform.position.z, target.position.z + camMov.maxCamPosZ, 
                                       Time.deltaTime * ( camMov.zoomDampingUp * ( relativeBikePos * 0.02f ) ) );
         
         minCamZoomZ        = Mathf.Lerp( camTransform.position.z, camPosZ, 
                                       Time.deltaTime * camMov.zoomDampingDown );
         
         speedMaxZoom    = Mathf.Lerp( camTransform.position.z, target.position.z + camMov.maxCamPosZ, 
                                       Time.deltaTime * ( camMov.zoomDampingGround * 0.02f ) );
         
         camZoomZ        = Mathf.Lerp( camTransform.position.z, camPosZ, 
                                       Time.deltaTime * camMov.zoomDampingGround );
         
         
         // Check if a crash was detected, and don't continue camera movements if it was
         if( GameStates.crashing )
         {
             return;
         }
         else
         {
             // We now create a new Vector3 for the camera using the variables above...
             if( CheckWheelCollision.hasFrontWC || CheckWheelCollision.hasRearWC )
             {
                 if( bikeVelocity.x > orthoCam.velocityTresholdX )
                     camZoom = speedMaxZoom;
                 else
                     camZoom = camZoomZ;
             }
             else if( !CheckWheelCollision.hasFrontWC && !CheckWheelCollision.hasRearWC )
             {
                 if( Mathf.Sign( bikeVelocity.y ) > 0 )
                     camZoom = maxCamZoomZ;
                 else
                     camZoom = minCamZoomZ;
             }
             
             // We now create a new Vector3 position for the 3D cam...
             camPosXYZ.z = camZoom;
         
             
             // Now let's define which dampening settings to use depending on the bike's movement
             if( target.position.x >= camRight - camMov.camOffsetX )
             {
                 camPosXYZ.x = camRight;
             }
             else
             {
                 camPosXYZ.x = camLeft;
             }
             
             // Which cam settings to use for vertical movements
             camPosXYZ.y = camGround;
             if( !goingUpHill || !Managers.PlayerCollision.cameraLockArea )
             {
                 if( !CheckWheelCollision.hasFrontWC && !CheckWheelCollision.hasRearWC )
                 {
                     if( target.position.y > camUp - camMov.camOffsetY )
                     {
                         camPosXYZ.y = camUp;
                     }
                     else
                     {
                         camPosXYZ.y = camDown;
                     }
                 }
             }
             if( goingUpHill || Managers.PlayerCollision.cameraLockArea )
             {
                 camPosXYZ.y = camGround;
             }
         }
 
         camTransform.position = camPosXYZ;
         camTransform.LookAt( new Vector3( camPosX, camPosY + 2, target.position.z));
         
         
         // ***************** THIS SECTION IS FOR ORBITING THE CAMERA ***************** //
         
         // Calculate the current rotation angles
         float wantedRotationAngle = target.eulerAngles.y;
         float currentRotationAngle = camTransform.eulerAngles.y;
         
         // Damp the rotation around the y-axis
         currentRotationAngle = Mathf.LerpAngle( currentRotationAngle, wantedRotationAngle, 2 * Time.deltaTime );
         
         // Convert the angle into a rotation
         Quaternion currentRotation = Quaternion.Euler( 0, currentRotationAngle, 0 );
         
         // Set the position of the camera on the x-z plane to distance meters behind the target
         /// That's the part I can't hook into my final smooth camera code, since I can only make it work when using 'target.position',
         /// but I really need to use the smooth camera position 'camPosXYZ' instead...
         Vector3 camPos = target.position - ( currentRotation * Vector3.forward * 21 );
         
         // ***************** END OF SECTION FOR ORBITING THE CAMERA ***************** //
     }

If you guys know another way to make my custom smooth camera movements orbit around a moving target, please do share hahaha!

Thanks for your time! Stephane

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 Noah-1 · May 13, 2012 at 03:49 AM

Here you go:

**Smooth Move / RotateAround**

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 AlucardJay · May 13, 2012 at 04:45 AM 0
Share

also : http://answers.unity3d.com/questions/246709/rotate-around-the-center-of-the-world-with-mouse.html

demo (hold right mouse button and move mouse, also use scroll wheel) : www.alucardj.net16.net/unityanswers/RotateCamAround.html

=]

avatar image ronronmx · May 13, 2012 at 06:56 AM 0
Share

Hey guys thanks for the links. I already looked at both these scripts, but they're both for mouse inputs and I can't figure out how to convert them to work with the position and rotation of my target object (no mouse input at all). The position I can probably put it together myself, but the rotation and distance is harder, and I'm having a hard time.

Taking my script above, what would make work the same way?

Thanks again!

avatar image whydoidoit · May 16, 2012 at 08:00 AM 0
Share

Would my script here help - if it's kind of what you need then I could talk you through it. It's got mouse input to change the angle, but it then auto-centres around the desired position and moves around to avoid obstacles obscuring the view. http://whydoidoit.com/2012/03/22/unity-terrain-avoiding-smart-follow-camera/

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Ideas for getting SmoothFollow camera to follow right behind? 0 Answers

Smoothfollow when the object changed 1 Answer

TPS Camera scipt.(C#) 0 Answers

Need help with script, one time touch to the screen = synchronize camera with face 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