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 Abacab · Feb 18, 2015 at 07:41 PM · c#cameracar

Laggy Camera

Hello!

I recently changed my camera script, and the new one is really laggy.

Here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class CarCamera : MonoBehaviour
 {
         [SerializeField]
         private float distanceAway;
         [SerializeField]
         private float distanceUp;
         [SerializeField]
         private float smooth;
         [SerializeField]
         private Transform follow;
         private Vector3 targetPosition;
 
     void Start() {
         follow = GameObject.FindWithTag ("Player").transform;
     }
 
     void OnDrawGizmos () {
 
     }
     
         void LateUpdate ()
         {
         targetPosition = follow.position + follow.up * distanceUp - follow.forward * distanceAway;
         Debug.DrawRay (follow.position, Vector3.up * distanceUp, Color.red);
         Debug.DrawRay (follow.position, -1f * follow.forward * distanceAway, Color.blue);
         Debug.DrawLine (follow.position, targetPosition, Color.magenta);
 
         transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
 
         transform.LookAt (follow);
 
         }
 }

And here is the inspector:

alt text

screen-shot-2015-02-18-at-213136.png (17.2 kB)
Comment
Add comment · Show 3
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 tanoshimi · Feb 19, 2015 at 05:11 PM 0
Share

So undo your changes and go back to your old camera script...?

Is your player being moved in FixedUpdate(), by any chance? If so, change your LateUpdate() code to FixedUpdate() as well to keep them in sync.

avatar image Abacab · Feb 19, 2015 at 05:35 PM 0
Share

The new camera would be miles better than the old one if it worked correctly.

And yes I'll try the FixedUpdate()!

EDIT: It is laggy even with FixedUpdate() :(

In case it helps, this is my Car's FixedUpdate()

 void FixedUpdate ()
         {
                 currentSpeed = 2 * 22 / 7 * wheelBL.radius * wheelBL.rpm * 60 / 100;
                 currentSpeed = $$anonymous$$athf.Round (currentSpeed);
 
                 if (currentSpeed <= 0) {
                         reverseButton.GetComponent<CanvasGroup> ().alpha = 1;
                         reverseButton.GetComponent<CanvasGroup> ().interactable = true;
                         reverseButton.GetComponent<CanvasGroup> ().blocksRaycasts = true;
 
                 } else {
                         reverseButton.GetComponent<CanvasGroup> ().alpha = 0;
                         reverseButton.GetComponent<CanvasGroup> ().interactable = false;
                         reverseButton.GetComponent<CanvasGroup> ().blocksRaycasts = false;
 
                 }
 
                 if (gasOn) {
                         started = true;
                         Gas ();
                 }
 
                 if (reverseOn) {
                         started = true;
                         Reverse ();
                 }
         }
avatar image hexagonius · Feb 19, 2015 at 06:51 PM 0
Share

You could also move the car by its velocity and enable interpolation mode on the rigidbody. That syncs the rendering too.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Feb 19, 2015 at 06:48 PM

I'd recommend usingSmoothDamp As it gives you control of how fast the camera "springs" towards the car like a rubber band.

Read about Lerp again. t needs to be a value interpolating from 0 to 1. You used Time.deltaTime * smooth as a value, which is a very jittery constant somewhere near 0 (e.g.: 0.05 times 4 = 0.2)

Comment
Add comment · Show 3 · 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 Abacab · Feb 19, 2015 at 06:55 PM 0
Share

It is lagging even if I change

 transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);


to

 transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smooth);
avatar image hexagonius · Feb 19, 2015 at 07:43 PM 0
Share

Save the canvasgroup to some variable on start and use it ins$$anonymous$$d of all the GetComponent calls

avatar image Abacab · Feb 19, 2015 at 10:04 PM 0
Share

That didn't cause any issues with the previous camera. :( But yes I will do that.

avatar image
0

Answer by ehsan_mohammadi · Apr 23, 2018 at 06:54 AM

Hi!

Change LateUpdate () function to FixedUpdate() and probably it's work.

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

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Camera turns to the front when reversing car 1 Answer

Reverse Camera Twitches 1 Answer

UNITY 3D: How to make the camera follow the player? Smoothly 2 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