Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jakekosmom · Oct 24, 2018 at 02:34 AM · cameraquaternioncamera rotatefirst-person-controllerthird-person-camera

Moving FPS/TPS camera to look at a target and resuming player control

I am having trouble with aligning my player controlled camera (I use the same class for FPS and TPS) to look at a target (when ResetCamera() is called by another script) and then getting the player to resume control. The main reason I am doing this is so I can switch between FPS and TPS cameras and remain looking at the same target.

I can look at the target fine, but ONLY if I stop setting the rotation based on yaw and pitch (from "Mouse X" and "Mouse Y" inputs) in LateUpdate() after I set the lookAtTarget in ResetCamera(), but that means the player can no longer look around. However, I cannot figure out how to get the correct yaw and pitch values after this so the player can continue looking around from the new look at target. How could I do this so the player could continue looking around?

 public class PlayerCamera : MonoBehaviour {
 
     public float mouseSensitivity = 10f;
     public Transform target;
     public float dstFromTarget = 2f;
     public Vector2 pitchConstraints = new Vector2(-20f, 85f);
 
     public float rotSmoothTime = .12f;
     Vector3 rotSmoothVel;
     Vector3 currRot;
 
     float yaw;
     float pitch;
     
     void LateUpdate() {
         yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
         pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
         pitch = Mathf.Clamp(pitch, pitchConstraints.x, pitchConstraints.y);
 
         currRot = Vector3.SmoothDamp(currRot, new Vector3(pitch, yaw), ref rotSmoothVel, rotSmoothTime);
         transform.eulerAngles = currRot;
 
         transform.position = target.position - transform.forward * dstFromTarget;
     }
 
     public void ResetCamera(Transform lookAtTarget) {
         transform.LookAt(lookAtTarget);
 
         // below gets yaw and pitch values that move the camera to look at the
         // wrong location
         // yaw = transform.eulerAngles.x;
         // pitch = transform.eulerAngles.y;
         // pitch = Mathf.Clamp(pitch, pitchConstraints.x, pitchConstraints.y);
         // currRot = new Vector3(pitch, yaw);
     }
 }
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
Best Answer

Answer by jakekosmom · Oct 24, 2018 at 06:17 AM

So, I figured out how to do what I wanted. Below is a script for a third or first person (just adjust the dstFromTarget variable to zero) camera that can be rotated to look at a target by another script with ResetCamera() and the player can resume moving around from that point.

 public class PlayerCamera : MonoBehaviour {

 public bool isFirstPerson = false;
 public float mouseSensitivity = 10f;
 public Transform target;
 public float dstFromTarget = 2f;
 public bool invertedPitch = false;
 public float smoothTime = .12f;

 public float minimumX = -85f;
 public float maximumX = 85f;

 // LateUpdate, so the Camera Target will definitely have been set
 void LateUpdate () {
     // first person updating is handled by Inverse kinematics stuff in my case
     if (isFirstPerson) return;
     UpdateStuff();
 }
 
 public void UpdateStuff() {
     float yaw = Input.GetAxisRaw("Mouse X");
     float pitch = -Input.GetAxisRaw("Mouse Y");
     if (invertedPitch) {
         pitch *= -1f;
     }

     Vector3 yRot = new Vector3(0f, yaw, 0f) * mouseSensitivity;
     Vector3 xRot = new Vector3(pitch, 0f, 0f) * mouseSensitivity;

     xRot = ClampRotationAroundXAxis(transform.rotation, xRot);

     Transform newTrans = transform;
     newTrans.Rotate(xRot);
     newTrans.Rotate(yRot, Space.World);

     transform.rotation = Quaternion.Slerp(transform.rotation, newTrans.rotation, smoothTime * Time.deltaTime);

     transform.position = target.position - transform.forward * dstFromTarget;
 }

 public void ResetCamera(Transform lookAtTarget) {
     transform.LookAt(lookAtTarget);
 }
 
 Vector3 ClampRotationAroundXAxis(Quaternion q, Vector3 xrot) {
     q.x /= q.w;
     q.y /= q.w;
     q.z /= q.w;
     q.w = 1.0f;

     float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);

     if (angleX < minimumX && xrot.x < 0f) {
         xrot = Vector3.zero;
     }
     if (angleX > maximumX && xrot.x > 0f) {
         xrot = Vector3.zero;
     }

     return xrot;
 }

}

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 Ajesh_Yohan · Jan 10, 2019 at 03:37 AM 0
Share

It works for me, but my character rotation and camera rotations are differed, will you give me the character script and camera prefab

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

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

Related Questions

Rotate player to screen pos 0 Answers

Coding noob struggles to implement first-Person camera rotation. 1 Answer

how do you sway the camera based on sharp turns 0 Answers

Rotate Camera/Leaning 1 Answer

How to rotate a camera locally around target? 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