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 Lele96 · Jun 29, 2020 at 04:16 PM · cameramovementcamera-movementballcamera follow

My game is laggy, but there are only a ball and some flat platforms.

Hi, I'm doing a simple smartphone game, where a ball moves horizontally with the movement of your finger (like RollingSky for example). I simply have a ball with this script:

 private float playerSpeed = 500;
 private float directionSpeed = 10;
 
 void Update()
 {
     GetComponent<Rigidbody>().velocity = Vector3.forward * playerSpeed * Time.deltaTime;

     Vector2 touch = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 16f));

     if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
     {
         transform.position = Vector3.Lerp(gameObject.transform.position, new 
         Vector3(Mathf.Clamp(touch.x, -15f, 15f), gameObject.transform.position.y, 
         gameObject.transform.position.z), directionSpeed * Time.deltaTime);
     }

 }


and the main camera with this script:

 float offset;
 public GameObject player;

 void Start()
 {
     offset = player.transform.position.z - transform.position.z;
 }

 
 void Update()
 {
         gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, new 
         Vector3(gameObject.transform.position.x, gameObject.transform.position.y, 
         player.gameObject.transform.position.z - offset), Time.deltaTime * 100);
         gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, new 
         Vector3(player.gameObject.transform.position.x, gameObject.transform.position.y, 
         gameObject.transform.position.z), Time.deltaTime * 5);


(I put the horizontal and vertical movement in two different lines because in the same the ball went too fast horizontally)

I don't know why the game is a little bit laggy, but there are only one ball and some flat platforms. I think it's the camera that goes wrong for some reason. What can I do? thank you!

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 logicandchaos · Jun 29, 2020 at 05:29 PM 0
Share

you should open up the profiler, a great chance to learn how to make your projects more efficient!

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kot2202 · Jun 29, 2020 at 05:58 PM

You are using GetComponent() in Update, try to assign it in Start() if it's possible. Also using lot of new Vector3. Generally it's good practice to create vector once and just edit it's xyz later. I think changing transform.position instead of velocity would be better on performance because you wouldn't have to calculate the physics of rigidbody. However opening the profiler should help more, these things affect performance a bit but I don't think they are the main problem

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 ttesla · Jun 29, 2020 at 07:47 PM

Your update logic seems quite strange. Using Lerps for the same variable twice and they are chained. I recommend comment out those and write a very basic follow code to test if thats the problem.

Also use LateUpdate() with camera stuff instead of Update(). And on the rigidybody of the ball, from the Interpolate settings. Set it to Interpolate or Extrapolate. This prevents jittering, if that is the thing happening.

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 BayonetCharge · Jun 30, 2020 at 12:12 AM

It might not have anything to do with performance (even if it can be improved).

Try doing movement in FixedUpdate().

https://connect.unity.com/p/why-we-add-forces-in-fixedupdate-not-in-update-eng

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

256 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 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 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 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 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 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 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 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 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 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 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 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

change objects Axis to match cameras rotation 0 Answers

Follow camera in 2d game 2 Answers

URGENT Cinemachine virtual camera while falling 1 Answer

Help make camera zoom smoother 3 Answers

Regarding transform.position in the roll a ball tutorial 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