• 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 /
  • Help Room /
avatar image
Question by rooster2 · Jan 08, 2017 at 03:23 PM · camerastuttershaking

camera wont stop stuttering

hi i am having problems with camera script where i added raycast to give it collision so it will get closer the the player when it comes into contact with terrain and now it wont stop stuttering but the camera collides with the wall and goes to the player perfect but the camera shaking is annoying, heres my code could someone tell me where i went and how i can fix it

using UnityEngine;

public class CameraMovement : MonoBehaviour {

 public Transform player_cam, center_point;

 public float distance, max_height, min_height, orbiting_speed, vertical_speed;

 float height;

 Vector3 dest;

 RaycastHit hit;

 void Update () {
     center_point.position = gameObject.transform.position + new Vector3 (0, 1.57f, 0);
     center_point.eulerAngles += new Vector3 (0, Input.GetAxis ("Mouse X") * Time.deltaTime * orbiting_speed, 0);
     height += Input.GetAxis ("Mouse Y") * Time.deltaTime * -vertical_speed;
     height = Mathf.Clamp (height, min_height, max_height);
 }

 void FixedUpdate () {
     dest = center_point.position + center_point.forward * -1 * distance + Vector3.up * height;
     if (Physics.Linecast (center_point.position, dest, out hit)) {
         if (hit.collider.CompareTag ("Terrain")) {
             player_cam.position = hit.point + hit.normal * 0.3f;
         }
     }
     player_cam.position = Vector3.Lerp (player_cam.position, dest, Time.deltaTime * 10);
     player_cam.LookAt (center_point);
 }

}

Comment

People who like this

0 Show 10
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 hoekkii · Jan 08, 2017 at 03:37 PM 0
Share

Don't set the position in FixedUpdate. use LateUpdate/Update instead

avatar image rooster2 hoekkii · Jan 08, 2017 at 03:46 PM 0
Share

And this should fix it if i put the player cam. Position in lateupdaye or update?

avatar image hoekkii rooster2 · Jan 08, 2017 at 04:01 PM 0
Share

I guess something like this:

 void LateUpdate()
     {
         dest = center_point.position + center_point.forward * -distance + Vector3.up * height;
 
         Vector3 v = dest - center_point.position;
         float magnitude = v.magnitude;
         v /= magnitude;
         Ray ray = new Ray(center_point.position, v);
 
         if (Physics.Raycast(ray, out hit, magnitude))
         {
             if (hit.collider.CompareTag("Terrain"))
             {
                 dest = hit.point + v * 0.3f;
             }
         }
         player_cam.position = Vector3.Lerp(player_cam.position, dest, Time.deltaTime * 10);
         player_cam.LookAt(center_point);
     }
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How I can make my camera keep a distance between two objects? 0 Answers

Rigidbody Based Third Person Controller Stutter Problem 0 Answers

How to sync RenderTextures so that they all render once per frame 0 Answers

Handheld Camera Shake (Constant Shake)? 1 Answer

Hey can someone help me with my camera script ? 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