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 xCeas · Dec 07, 2013 at 03:51 PM · camerathird-personmousemove

third person camera

Hello I have a third person camera that -when not moving a player can freely look around the character -otherwise it goes behind the character (lookat) and on moving the mouse the character rotates, but I try to achieve that when the player stop moving the camera remains still when it last was here is the code:

if(target && !GuiToggled()) {

         x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;

// y -= Input.GetAxis("Mouse Y") ySpeed 0.02f;

// y = ClampAngle(y, yMinLimit, yMaxLimit);

         Quaternion rotation = Quaternion.Euler(0, x, 0);
         
         // calculate the desired distance 
         desiredDistance -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance); 
         desiredDistance = Mathf.Clamp (desiredDistance, minDistance, maxDistance); 
         correctedDistance = desiredDistance; 
         
         vTargetOffset = new Vector3 (0, -targetHeight, 0);
         Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset); 
         
         RaycastHit collisionHit; 
         Vector3 trueTargetPosition = new Vector3 (target.position.x, target.position.y + targetHeight, target.position.z); 

         // if there was a collision, correct the camera position and calculate the corrected distance 
         bool isCorrected = false; 
         if (Physics.Linecast (trueTargetPosition, position, out collisionHit, collisionLayers.value)) 
         { 
             // calculate the distance from the original estimated position to the collision location,
             // subtracting out a safety "offset" distance from the object we hit.  The offset will help
             // keep the camera from being right on top of the surface we hit, which usually shows up as
             // the surface geometry getting partially clipped by the camera's front clipping plane.
             correctedDistance = Vector3.Distance (trueTargetPosition, collisionHit.point) - offsetFromWall; 
             isCorrected = true;
         }

         // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance 
         currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance; 

         // keep within legal limits
         currentDistance = Mathf.Clamp (currentDistance, minDistance, maxDistance); 

            // recalculate position based on the new currentDistance 
         position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset); 
         _myTransform.rotation = rotation;
         _myTransform.position = position;
         if(moving)
         {
             RotateMovement();
         }

 private void RotateMovement()
     {
         float rotateSpeed = 5f;
         float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
         target.transform.Rotate(0, horizontal, 0);
         float desiredAngle = target.transform.eulerAngles.y;
         Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
         transform.position = target.transform.position - (rotation * offset);
         transform.LookAt(GameObject.Find("Lookat").transform);
         lastRotation = transform.rotation;
     }
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
1
Best Answer

Answer by Fornoreason1000 · Dec 07, 2013 at 04:42 PM

basically you want to stop the camera from Automatically moving, which is caused by an automatic angle being set. that would be these lines here,

         public Transform target;
         public float x;
         float xSpeed = 20;
         float zoomRate = 2f;
         float desiredDistance;
         float correctedDistance;
         float targetHeight;
         float minDistance = 3;
         float maxDistance = 12;
         float offsetFromWall = .3f;
         float currentDistance;
         float zoomDampening = 0.1f;
         public Vector3 offset;
        public  float desiredAngle;
         public bool moving = true;
         Quaternion lastRotation;
     
       Vector3  vTargetOffset = Vector3.up;
     
       public void LateUpdate()
       {
     
           if (target)
           {
     
               x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
               // y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
     
               // y = ClampAngle(y, yMinLimit, yMaxLimit);
     
               Quaternion rotation = Quaternion.Euler(0, x, 0);
     
               // calculate the desired distance 
               desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
               desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
               correctedDistance = desiredDistance;
     
               vTargetOffset = new Vector3(0, -targetHeight, 0);
               Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset);
     
               RaycastHit collisionHit;
               Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y + targetHeight, target.position.z);
     
               // if there was a collision, correct the camera position and calculate the corrected distance 
               bool isCorrected = false;
               if (Physics.Linecast(trueTargetPosition, position, out collisionHit))
               {
                   // calculate the distance from the original estimated position to the collision location,
                   // subtracting out a safety "offset" distance from the object we hit.  The offset will help
                   // keep the camera from being right on top of the surface we hit, which usually shows up as
                   // the surface geometry getting partially clipped by the camera's front clipping plane.
                   correctedDistance = Vector3.Distance(trueTargetPosition, collisionHit.point) - offsetFromWall;
                   isCorrected = true;
               }
     
               // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance 
               currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
     
               // keep within legal limits
               currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
     
               // recalculate position based on the new currentDistance 
               position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset);
               transform.rotation = rotation;
               transform.position = position;
     
               RotateMovement();
             
           }
       }
     private void RotateMovement()
         {
            float rotateSpeed = 5f;
            float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
     
          
             if (moving)
             {
                 x = 0;
                 target.transform.Rotate(0, horizontal, 0);
                 desiredAngle = target.transform.eulerAngles.y;
             }
     
             Quaternion rotation = Quaternion.Euler(0, desiredAngle + x, 0);
             transform.position = target.transform.position - (rotation * offset);
             transform.LookAt(GameObject.Find("LookAt").transform);
            lastRotation = transform.rotation;
         }
     }
 
                 

then make rotation and moving a member wise variable. also removing the If(moving) in your original code that way the RotateFunction is called to apply the movements sent by the player. nice script btw.

EDIT: the whole time i thought this was an orbit script, it was really a follow.

Comment
Add comment · Show 13 · 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 xCeas · Dec 07, 2013 at 04:49 PM 0
Share

thanks for the comment Im new to this part of unity (angles, vectors and such) so if i basicly want to set my own rotation i just use the last rotation convert it to an angle and calculate with that the not moving rotation?

avatar image Fornoreason1000 · Dec 07, 2013 at 04:56 PM 0
Share

yes if you want to do it that way can:

   if(!moving)
                 {
          rotation = Quaternion.Euler(0, desiredAngle, 0); 
          //stop this line when player is stationary 
                  }
       else {
        rotation = Quaternion.Euler(lastRotation.euler + new Vector3 (0, x, 0));
     }

sorry for the poor formatting, this editor hates me. also you may want to do something about that input x += , you adding 1 * speed each frame, so your camera may go spazzo, if it does let me know.

avatar image xCeas · Dec 07, 2013 at 05:00 PM 0
Share

thanks so much for the help gonna try it right now :)

avatar image xCeas · Dec 07, 2013 at 05:08 PM 0
Share
 private void Rotate$$anonymous$$ovement()
     {
         float rotateSpeed = 5f;
         float horizontal = Input.GetAxis("$$anonymous$$ouse X") * rotateSpeed;
         target.transform.Rotate(0, horizontal, 0);
         float desiredAngle = target.transform.eulerAngles.y;
         if(!moving)
         {
             rotation = Quaternion.Euler(0, desiredAngle, 0); 
          //stop this line when player is stationary 
         }
         else
         {
             rotation = Quaternion.Euler(lastRotation.eulerAngles + new Vector3 (0, x, 0));
         }
         transform.position = target.transform.position - (rotation * offset);
         transform.LookAt(GameObject.Find("Lookat").transform);
         lastRotation = transform.rotation;
     }

my camera is now looking bumping up and down

avatar image Fornoreason1000 · Dec 07, 2013 at 05:11 PM 0
Share

does it only jitter when you move the camera? does the camera move? does it only jitter when you stop moving the player? try this;

 float desiredAngle = target.transform.eulerAngles.y;
     if(!moving) { 
     desiredAngle = 0; 
     }
 rotation = Quaternion.Euler(0, desiredAngle + x, 0));
             

  

Show more comments

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

17 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 avatar image avatar image

Related Questions

Make the camera stay behind the third person 1 Answer

Prevent third person camera from clipping through terrain and objects 2 Answers

Drawing architecture normally, but with key items only visible in front of the player 1 Answer

ThirdPersonShooter-Camera-Script Optimization 0 Answers

third person camera script? 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