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 DRRosen3 · Nov 27, 2014 at 03:57 PM · cameracamera-movementplayer movement

Player Movement / Camera Tracking Issue

The problem I'm having is that while the player is in motion the camera is jerking back and forth between the player.transform.position and where it's SUPPOSED to be.

Here is the Camera script...

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
 
     public Transform cameraTarget;
     private float x = 0.0f;
     private float y = 0.0f;
 
     private int mouseXSpeedMod = 5;
     private int mouseYSpeedMod = 3;
 
     public float maxViewDistance = 25;
     public float minViewDistance = 1;
     public int zoomRate = 30;
     private int lerpRate = 2;
     private float distance = 3;
     private float desiredDistance;
     private float correctedDistance;
     private float currentDistance;
 
     public float cameraTargetHeight = 1.5f; //This number is the height of the camera's target. i.e. The player.
 
     void Start () {
         cameraTarget = GameObject.FindGameObjectWithTag("Player").transform;
         Vector3 angles = transform.eulerAngles;
         x = angles.x;
         y = angles.y;
 
         currentDistance = distance;
         desiredDistance = distance;
         correctedDistance = distance;
     }
 
     void Update(){
 
     }
 
     void LateUpdate () {
         if(Input.GetMouseButton (1)){
             x += Input.GetAxis("Mouse X") * mouseXSpeedMod;
             y -= Input.GetAxis("Mouse Y") * mouseYSpeedMod;
         }
 
         else if(Input.GetAxis("Movement") != 0 || Input.GetAxis("Rotate Player") != 0){
             float targetRotationAngle =  cameraTarget.eulerAngles.y;
             float cameraRotationAngle = transform.eulerAngles.y;
             x = Mathf.LerpAngle(cameraRotationAngle, targetRotationAngle, lerpRate * Time.deltaTime);
         }
 
         y = ClampAngle(y, -50, 80);
 
         Quaternion rotation = Quaternion.Euler(y, x, 0);
 
         desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance);
         desiredDistance = Mathf.Clamp(desiredDistance, minViewDistance, maxViewDistance);
         correctedDistance = desiredDistance;
 
         Vector3 position = cameraTarget.position - (rotation * Vector3.forward * desiredDistance);
 
         RaycastHit collisionHit;
         Vector3 cameraTargetPosition = new Vector3(cameraTarget.position.x, cameraTarget.position.y + cameraTargetHeight,cameraTarget.position.z);
 
         bool isCorrected = false;
         if(Physics.Linecast(cameraTargetPosition, position, out collisionHit)){
             position = collisionHit.point;
             correctedDistance = Vector3.Distance(cameraTargetPosition, position);
             isCorrected = true;
         }
 
         currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomRate) : correctedDistance;
 
         position = cameraTarget.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -cameraTargetHeight, 0));
 
         transform.rotation = rotation;
         transform.position = position;
     }
 
     private static float ClampAngle(float angle, float min, float max){
         if(angle < -360){
             angle += 360;
         }
         if(angle > 360){
             angle -= 360;
         }
         return Mathf.Clamp(angle, min, max);
     }
 }

And here is the Player Movement script...

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof(CharacterController))]
 public class Movement : MonoBehaviour {
 
     public float rotateSpeed = 250f;
     public float moveSpeed = 5f;
 
     private CharacterController controller;
     private Transform myTransform;
     private Animator anim;
 
     void Awake(){
         controller = GetComponent<CharacterController>();
         myTransform = transform;
     }
 
     void Start () {
         anim = GetComponent<Animator>();
     }
 
     void Update () {
         Turn();
         Walk();
     }
 
     private void Turn(){
         if(Mathf.Abs(Input.GetAxis("Rotate Player")) > 0){
             myTransform.Rotate(0, Input.GetAxis("Rotate Player") * Time.deltaTime * rotateSpeed, 0);
             anim.SetFloat("Direction", Input.GetAxis("Rotate Player"));
         }else{
             anim.SetFloat("Direction", 0);
         }
     }
 
     private void Walk(){
         if(Mathf.Abs(Input.GetAxis("Movement")) > 0){
             controller.SimpleMove(myTransform.TransformDirection(Vector3.forward) * Input.GetAxis("Movement") * moveSpeed);
             anim.SetFloat("Speed", Input.GetAxis("Movement"));
         }else{
             anim.SetFloat("Speed", 0);
         }
     }
 
 }


In case it matters... I have a game object called GameMaster which instantiates the player at the start of the scene. Then it instantiates the camera.

Comment
Add comment · Show 1
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 DRRosen3 · Nov 27, 2014 at 04:39 PM 0
Share

Alright, so growing impatient for responses I went through my $$anonymous$$ovement script piece by piece (commenting out lines and re-adding them little-by-little). The camera jerking back and forth doesn't start happening until I add in the anim.SetFloat("Speed", Input.GetAxis("$$anonymous$$ovement"))...any thoughts?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Moving Player Relative to Camera? 1 Answer

Camera problem 2 Answers

How to move the player object in the same direction as the input axis, considering the cameras direction? 0 Answers

camera script not working 1 Answer

Make a ridgid body move in the direction the camera is facing 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