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 TrevTS · Apr 18, 2018 at 10:20 PM · camerarotationcamera rotatequaternions3rd person camera

Unexpected Camera Rotation

Hey all, I've been writing a custom third person controller and I almost have the camera working the way I want for basic movement. The problem I am having is that whenever I rotate the camera with the right stick, and then move the player causing the camera to follow the player, when I go to rotate the camera again the camera tries to rotate to its last known rotation spot. I hope I worded that well enough. I have added my camera script and will provide screenshots if necessary.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraFromTutorial : MonoBehaviour {
 
     public float X_ANGLE_MIN, X_ANGLE_MAX;
     public bool cameraIsMovingByInput = false;
     [SerializeField]
     private float distanceAway, distanceUp;
     [SerializeField]
     private float camSmoothDampTime = 0.1f;
     [SerializeField]
     private Transform followTransform;
     [SerializeField]
     private Vector3 offset = new Vector3(0f, 1.5f, 0f);
     
     private Vector3 targetPosition;
     private Vector3 lookDir;
     private Vector3 velocityCamSmooth = Vector3.zero;
     private Vector3 input = Vector3.zero;
     private Vector3 cameraRotatePosition;
 
     // Use this for initialization
     void Start () {
         // Follow Transform is the player
         followTransform = followTransform.transform;
     }
     
     // Update is called once per frame
     void Update () {
         TrackInput ();
     }
 
     void LateUpdate () {
         // If the player isnt moving the right stick
         if (!cameraIsMovingByInput) {
             CameraFollow();
         } else {
             MoveCamera();
         }
 
         // Always Look at the player
         transform.LookAt(followTransform);
     }
 
     void TrackInput () {
         // This will just get the input values
         input.x = Input.GetAxis ("RHorizontal");
         input.y = Input.GetAxis  ("RVertical");
         input.z = 0f;
         // Set cameraIsMovingByInput to True if input has value else false
         if(input != Vector3.zero) {
             cameraIsMovingByInput = true;
         } else { 
             cameraIsMovingByInput = false;
         }
 
         if (input.sqrMagnitude > 1f) {
             input.Normalize ();
         }
 
         // We add this to the cameraInputPosition to know where to rotate to
         cameraRotatePosition.x += input.y;
         cameraRotatePosition.y += input.x;
 
         // Clamp the x so we cant rotate to above the players head or below his feet
         cameraRotatePosition.x = Mathf.Clamp(cameraRotatePosition.x, X_ANGLE_MIN, X_ANGLE_MAX);
     }
 
     void MoveCamera () {
         // Set the rotation relative to the input
         Quaternion rotation = Quaternion.Euler(cameraRotatePosition.x, cameraRotatePosition.y, 0);
         transform.position = Vector3.Lerp(transform.position, (followTransform.position + rotation * new Vector3(0f, 0f, -distanceAway)), Time.deltaTime * 3);
         Debug.DrawRay(transform.position, transform.forward * 3, Color.cyan, 1f);
     }
 
     void CameraFollow () {
         Vector3 characterOffset = followTransform.position + offset;
 
         lookDir = characterOffset - transform.position;
         lookDir.y = 0;
         lookDir.Normalize();
         Debug.DrawRay(transform.position, lookDir, Color.green);
 
         // Setting the target position by moving up by follow.up * distanceUp and back by follow.forward * distanceAway 
         targetPosition = characterOffset + followTransform.up * distanceUp - lookDir * distanceAway;
 
         SmoothPosition(transform.position, targetPosition);
     }
 
     private void SmoothPosition(Vector3 fromPos, Vector3 toPos) {
         transform.position = Vector3.SmoothDamp(fromPos, toPos, ref velocityCamSmooth, camSmoothDampTime);
     }
 }
 
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

147 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

Related Questions

Forward and back movements with a camera emulating an isometric view 1 Answer

Mouse axes based on position, not movement. 1 Answer

How to set horizon level for camera orbit script? 0 Answers

Camera rotates when I run into a wall 0 Answers

Third person follow camera on a sphere 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