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 Rorin · Jan 15, 2014 at 04:59 AM · cameraplayercubeshake

When moving player[box] camera shakes. Help

Hi everyone, I'm having this problem with my player char. when moving. I have attached a camera to the Player [ a Cube ] and when I walk the camera shakes. I understand why this is happening, because it's a cube and when I walk it doesn't have smoothness or something... but I don't know how to fix this, what should I do ? Here is my code attached to the player but I don't think that there is the problem:

 public class TransformFunctions : MonoBehaviour { 
 
     public float rotateSpeed;
     public float speed;
     public float timeLeft;
     public int counter;
     public float timercount;
     
     private Light myLight;
     private float lRange = 2.5f;
     private float orange = 8f;
 
     public AudioClip pick;
     public AudioClip recharge;
     public TextMesh Bateries;
     public TextMesh timeLeft2die;
     public TextMesh endText;
     public TextMesh WinText;
     
     // Called when the game starts.
     void Start()
     {
         myLight = GetComponent<Light> ();
         counter = 0;
         timeLeft2die.text = "Time: "+timercount;
         endText.text = "";
         WinText.text = "";
     }
 
     // Update is called once per frame !!!!!
     void Update () 
     {
         ColorChange ();
         Movement ();
         SetCountText ();
         lightSubtract ();
         TimeLeft ();
         OnTriggerExit ();
         ExitApp ();
     }
     /// collision detection
 
     void OnTriggerEnter (Collider other)
     {
         if (other.gameObject.tag == "End") {
 
                         gameObject.GetComponent<TransformFunctions> ().enabled = false;
                         WinText.text = "You Win\n press ESC to exit";
                 }
         if (other.gameObject.tag == "PickUp") 
         {
             other.gameObject.SetActive(false);
             audio.PlayOneShot(pick, 0.8f);
             counter = counter + 1;
             Bateries.text = "Bateries: "+counter;
 
         }
     }
         //bateries count
     void SetCountText()
     {
 
         if (Input.GetKeyDown (KeyCode.R) && counter > 0) {
                         counter = counter - 1;
             myLight.light.intensity = light.intensity + 2.5f;
             audio.PlayOneShot(recharge);
                 }
 
         Bateries.text = "Bateries: " + counter;
             while(counter < 0){
                 counter  = 0;
             }
     
     }
 
         void TimeLeft()
         {
             if (timercount > 0) {
             timercount -= Time.deltaTime;
                 }
             while (timercount < 0) {
                         timercount = 0;
                 }
         timeLeft2die.text = "Time: " + timercount;
         }
          void OnTriggerExit (){
 
         if (timercount == 0 && gameObject.tag == "Player") {
             gameObject.GetComponent<TransformFunctions>().enabled = false;
             endText.text = "GAME OVER";
                 }
     }
             //movement
             void Movement(){
                 if (Input.GetKey (KeyCode.UpArrow)) {
                         transform.Translate (Vector3.forward * speed * Time.deltaTime);
                 }
                 if (Input.GetKey (KeyCode.DownArrow)) {
                         transform.Translate (Vector3.back * speed * Time.deltaTime);
                 }
                 if (Input.GetKey (KeyCode.LeftArrow)) {
                         transform.Rotate (Vector3.down * rotateSpeed * Time.deltaTime);
                 }
                 if (Input.GetKey (KeyCode.RightArrow)) {
                         transform.Rotate (Vector3.up * rotateSpeed * Time.deltaTime);
                 }
                 if (Input.GetKeyDown (KeyCode.Space)) {
                         myLight.enabled = !myLight.enabled;
                 }
                 if (Input.GetKeyDown (KeyCode.LeftAlt)) {
                         myLight.type = LightType.Point;
                         myLight.range = lRange;
                 } else if (Input.GetKeyDown (KeyCode.LeftShift)) {
                         myLight.type = LightType.Spot;
                         myLight.range = orange;
                 }
         }
          void ExitApp(){
                 if (Input.GetKey (KeyCode.Escape)) {
                         Application.Quit ();
                 }
     }
         //flashlight color change
         void ColorChange(){
         if (Input.GetKey (KeyCode.Keypad0) || Input.GetKey (KeyCode.F1)) {
                         myLight.light.color = Color.cyan;
                 }
         if (Input.GetKey (KeyCode.Keypad1)|| Input.GetKey (KeyCode.F2)) {
             myLight.light.color = Color.red;
         }
         if (Input.GetKey (KeyCode.Keypad2)|| Input.GetKey (KeyCode.F3)) {
             myLight.light.color = Color.yellow;
         }
         if (Input.GetKey (KeyCode.Keypad3)|| Input.GetKey (KeyCode.F4)) {
             myLight.light.color = Color.green;
         }
         if (Input.GetKey (KeyCode.Keypad4)|| Input.GetKey (KeyCode.F5)) {
             myLight.light.color = Color.magenta;
         }
         }
     void lightSubtract ()
     {
         myLight.light.intensity -=Time.deltaTime * 0.1f;
     }
 }

How to remove the camera shake when walking with the Player [ cube ] ? Thanks!

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 nesis · Jan 15, 2014 at 05:26 AM 0
Share

There's a bit too much code for me to read through right now, but you might find putting movement code for the camera in LateUpdate() ins$$anonymous$$d of Update() could solve your problem.

Alternately, you could give your camera a unique smoothed follow script that has small enough lag behind player movement for collider juddering to be unnoticeable.

I'd suggest trying something like currentCameraPosition = Vector3.Lerp(currentCameraPosition,idealCameraPosition,lerpValue) (where lerpValue is between 0 and 1, ideally as high of a value as you can get away with without juddering).

If the camera lagging too far behind the player becomes an issue, you can make lerpValue's value dependent on distance to the ideal camera position:

 const float $$anonymous$$inLerpValue = 0.3f;
 const float $$anonymous$$axDist = 10f;
 float curDist = Vector3.Distance(currentCameraPosition,idealCameraPosition);
 float lerpValue = $$anonymous$$athf.Clamp01(curDist/$$anonymous$$axDist); //sets lerpValue to 1 if curDist>maxDist
 if (lerpValue<$$anonymous$$inLerpValue) lerpValue = $$anonymous$$inLerpValue;

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sanmn19 · Jan 15, 2014 at 08:05 AM

Change from box collider to sphere collider for cube. Also, add a rigidbody to the camera and freeze it rotation in all axes in inspector. The camera shakes because of the friction between cube and the surface.

Comment
Add comment · Show 5 · 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 Rorin · Jan 15, 2014 at 04:06 PM 0
Share

Thank you mate but how can I freeze the rotation? I've attached a camera in the Player gameObject throught the add component menu. Now I have to make a new camera ? What exactly?

avatar image sanmn19 · Jan 15, 2014 at 04:33 PM 0
Share

I want to know if you want the sphere to rotate while moving? If the answer is no, then you need to

  1. Create an empty gameObject. Add a sphere collider component approximately equal to the size of the cube.

  2. $$anonymous$$ake your cube a child of the gameObject created in the above step.

  3. For the cube attach a rigidbody and freeze the rotation along x y and z. It can be done in the inspector.

  4. Now attach the main camera in the scene to you cube.

So the heirarchy is as follows:

  • EmptygameObject
    • Cube - mainCamera

If the answer is yes:

  1. Remove box collider for cube. Attach sphere collider of approx. Same size.

  2. $$anonymous$$ake main camera as a child of the cube.

  3. Add rigidbody to the camera. Freeze rotation along x y and z axis from the inspector.

The heirarchy is as follows:

Cube - mainCamera

avatar image sanmn19 · Jan 15, 2014 at 04:37 PM 0
Share

When I say attach the mainCamera I mean drag and drop the main camera to the desired gameObject to make it its child.

avatar image Rorin · Jan 15, 2014 at 04:38 PM 0
Share

Thanks mate but I just added this to my Update() function :) - transform.eulerAngles = new Vector3(0,transform.eulerAngles.y,0);

And it worked just like I wanted it to ! :) No camera shaking! And I changed the box collider to Sphere collider just like you said! Thanks for all !!!!! I really appreciate IT !!!!!!!

BTW this is the DL link to the game - http://www50.zippyshare.com/v/18838542/file.html

you can check it and see how everything is working :)

avatar image sanmn19 · Jan 15, 2014 at 04:40 PM 0
Share

You are welcome. If you found answrr to be correct, please mark it as correct.

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

20 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

Related Questions

Camera shake while following Player 1 Answer

Make camera blurry/shaking when enemy gets close 1 Answer

Camera Shaking Problem 1 Answer

C# - Offset In-Air Camera from Current Object Position 1 Answer

Input Controls Camera on a Predetermined Path. 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