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 /
This question was closed Aug 12, 2014 at 08:48 AM by thornekey for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by thornekey · Aug 11, 2014 at 02:59 PM · orbit3rd person controller

Orbital Camera Spazzing out uncontrollably..

Hey all, Im currently working on a 3rd person camera. Kind of like in WoW. Anyway, ive got no errors but as soon as i hit play, the camera spazzes out uncontrollably like it doesnt know where to go, and it shifts the position from what seems to be the min position allowance to the current position, every frame.

here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class Third_Person_Camera : MonoBehaviour {
 
     public static Third_Person_Camera Instance;
     public Transform TargetLookAt;
     public float distanceCurr = 5f;
     public float distanceMin = 3f;
     public float distanceMax = 10f;
 
     public float distanceSmooth = 0.05f;
 
     public float xMouseSensitivity = 5f;
     public float yMouseSensitivity = 5f;
     public float mouseWheelSensitivity = 5f;
 
     public float xSmooth = 0.05f;
     public float ySmooth = 0.1f;
 
     public float yMinLimit = -40f;
     public float yMaxLimit = 80f;
 
 
     private float mouseX = 0f;
     private float mouseY = 0f;
     private float velX = 0f;
     private float velY = 0f;
     private float velZ = 0f;
     private float velDistance = 0f;
     private float startDistance = 0f;
 
     private Vector3 position = Vector3.zero;
 
     private Vector3 desiredPosition = Vector3.zero;
 
     private float desiredDistance = 0f;
 
 
     void Awake () {
         Instance = this;
     }
 
     void Start () {
         distanceCurr = Mathf.Clamp (distanceCurr, distanceMin, distanceMax);
         startDistance = distanceCurr;
         Reset ();
     }
     
     void LateUpdate () {
         if (TargetLookAt == null) {
             return;
         }
 
         HandlePlayerInput ();
         CalculateDesiredPosition ();
         UpdatePosition ();
     }
 
     void HandlePlayerInput () { 
         var deadZone = 0.01f;
 
         //this checks to see if the right mouse is down.
         //later check to see if also not over GUI.
         if (Input.GetMouseButtonDown (1)) {
             mouseX += Input.GetAxis("Mouse X") * xMouseSensitivity;
             mouseY -= Input.GetAxis("Mouse Y") * yMouseSensitivity;
         }
 
         //clamp or limit mouse rotation for Y-Axis.
         mouseY = camHelper.clampAngle (mouseY, yMinLimit, yMaxLimit);
 
         if (Input.GetAxis ("Mouse ScrollWheel") < -deadZone || Input.GetAxis ("Mouse ScrollWheel") > deadZone) {
             desiredDistance = Mathf.Clamp(distanceCurr - 
                                           Input.GetAxis ("Mouse ScrollWheel") * 
                                           mouseWheelSensitivity, 
                                           distanceMin, distanceMax);
         }
     }
 
     void CalculateDesiredPosition () { 
         //evaluate the players distance.
         distanceCurr = Mathf.SmoothDamp (distanceCurr, desiredDistance, ref velDistance, distanceSmooth);
 
         //then calculate the desired position..
         desiredPosition = CalculatePosition (mouseY, mouseX, distanceCurr);
     }
 
     Vector3 CalculatePosition (float rotationX, float rotationY, float distance) {
         Vector3 direction = new Vector3 (0, 0, -distance);
         Quaternion rotation = Quaternion.Euler (rotationX, rotationY, 0);
         return TargetLookAt.position + rotation * direction;
     }
 
     void UpdatePosition () { 
         
         var posX = Mathf.SmoothDamp (position.x, desiredPosition.x, ref velX, xSmooth);
         var posY = Mathf.SmoothDamp (position.y, desiredPosition.y, ref velY, ySmooth);
         var posZ = Mathf.SmoothDamp (position.z, desiredPosition.z, ref velZ, xSmooth);
 
         transform.position = new Vector3 (posX, posY, posZ);
 
         transform.LookAt (TargetLookAt);
     }
 
     public void  Reset () {
         mouseX = 0;
         mouseY = 10;
         distanceCurr = startDistance;
         desiredDistance = distanceCurr;
     }
 
     public static void UseExistingOrCreateNewMainCamera () {
         GameObject tempCamera;
         GameObject targetLookAt;
         Third_Person_Camera myCamera;
 
         if (Camera.mainCamera != null) {
             tempCamera = Camera.mainCamera.gameObject;
         } else { 
             tempCamera = new GameObject("Main Camera");
             tempCamera.AddComponent("Camera");
             tempCamera.tag = ("MainCamera");
         }
 
         tempCamera.AddComponent ("Third_Person_Camera");
         myCamera = tempCamera.GetComponent ("Third_Person_Camera") as Third_Person_Camera;
 
         targetLookAt = GameObject.Find ("targetLookAt") as GameObject;
 
         if (targetLookAt == null) {
             targetLookAt = new GameObject("targetLookAt");
             targetLookAt.transform.position = Vector3.zero;
         }
 
         myCamera.TargetLookAt = targetLookAt.transform;
     }
 }


the error has something to do with this:

I have isolated the issue to this:

         var posX = Mathf.SmoothDamp (position.x, desiredPosition.x, ref velX, xSmooth);
         var posY = Mathf.SmoothDamp (position.y, desiredPosition.y, ref velY, ySmooth);
         var posZ = Mathf.SmoothDamp (position.z, desiredPosition.z, ref velZ, xSmooth);
 
         transform.position = new Vector3 (posX, posY, posZ);

dont say its because on posZ one of the variable params should be zSmooth, cos thats not right.

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by thornekey · Aug 12, 2014 at 01:19 AM

I have isolated the issue to this:

         var posX = Mathf.SmoothDamp (position.x, desiredPosition.x, ref velX, xSmooth);
         var posY = Mathf.SmoothDamp (position.y, desiredPosition.y, ref velY, ySmooth);
         var posZ = Mathf.SmoothDamp (position.z, desiredPosition.z, ref velZ, xSmooth);
 
         transform.position = new Vector3 (posX, posY, posZ);
Comment
Add comment · 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
0

Answer by DajBuzi · Aug 11, 2014 at 03:31 PM

Hello,

For such easy task i think that you have wrote to much code, try this one:

 public class SphericalCam : MonoBehaviour {
     public float MinDist, CurrentDist, MaxDist, TranslateSpeed, AngleH, AngleV;
     public Transform Target;

     public void Update(){
         AngleH += Input.GetAxis("Mouse X");
         AngleV -= Input.GetAxis("Mouse Y");

         CurrentDist += Input.GetAxis("Mouse ScrollWheel");
     }

     public void LateUpdate(){
         Vector3 tmp;
         tmp.x = (Mathf.Cos(AngleH * (Mathf.PI / 180)) * Mathf.Sin(AngleV * (Mathf.PI / 180)) * CurrentDist + Target.position.x;
         tmp.z = (Mathf.Sin(AngleH * (Mathf.PI / 180)) * Mathf.Sin(AngleV * (Mathf.PI / 180)) * CurrentDist + Target.position.z;
         tmp.y = Mathf.Sin(AngleV * (Mathf.PI / 180)) * CurrentDist + Target.position.y;

         transform.position = Vector3.Slerp(transform.position, tmp, TranslateSpeed * Time.deltaTime);

         transform.LookAt(Target);
     }
 }


That should run smoothly ;)

Regards, M.Rogalski

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 thornekey · Aug 11, 2014 at 10:47 PM 0
Share

But then, what is the issue in the code I have?

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Smooth Mouse Orbit 1 Answer

Rotate an object around another on input 0 Answers

Orbiting w/ Touch, Camera "snaps" 180 at north & south pole (webplayer link) 0 Answers

Android deltaPosition.y isn't working? 1 Answer

How to orbit a camera around an object with device(mobile) tilting/accelatarion? 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