Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bySharp · Jan 16, 2016 at 01:30 PM · problem during runtimehealth

Lag Problem in 2D Game

I'm a beginner so I started watching tutorials and I've been following my first one, but the result is different from the guy on youtube. My character lags from time to time, the animations were fine when i built them (the character stood in place), but now that it moves it's weird. His player is responsive but mine takes a time to move and like twitches back and forth from time to time. I guess this has something to do with either rigidbody2d settings, textures or script. Here's the script:

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     public int maxSpeed = 3;
     public float speed = 50f;
     private Animator anim;
     private Rigidbody2D rb2d;
 
     void Start () {
         anim = gameObject.GetComponent<Animator>();
         rb2d = gameObject.GetComponent<Rigidbody2D>();
     }
     
     
     void Update () {
         anim.SetFloat("speed", Mathf.Abs(Input.GetAxis("Horizontal")));
         if (Input.GetAxis("Horizontal") < -0.1f)
         {
             transform.localScale = new Vector3(-1, 1, 1);
         }
 
         if (Input.GetAxis("Horizontal") > 0.1f)
         {
             transform.localScale = new Vector3(1, 1, 1);
         }
     }
 
     void FixedUpdate()
     {
         float h=Input.GetAxis("Horizontal");
         rb2d.AddForce(Vector2.right*speed*h);
 
         if (rb2d.velocity.x > maxSpeed)
             rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
 
         if (rb2d.velocity.x < -maxSpeed)
             rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
             
     }
 }

Maybe it's the way i imported the images, they may take up too much memory: i set them all to Pixels per unit: 32 or 10, Filter: Point(no filter), Format True-Color I guess it could also be from collider, since the problem happens when i move. I set box colliders.

Please help me.

Edit: Also, in Visual Studio there's a warning: Warning CS0649 Field 'Player.anim' is never assigned to, and will always have its default value null

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Jan 16, 2016 at 02:26 PM

I have a few suggestions:

  • Don't scale the object you're moving. Make your Animator gameobject a child and "rotate" it 180°. Leave the moving gameobject as is.

  • Add a PhysicsMaterial2D to the player collider. Set its values to 0 so there's no friction.

  • Disable root motion on the animator

The warning from VS shouldn't be there since the variable is assigned to in Start(). Try syncing VS by closing it and then in Unity under "Assets" select "Open C# Project"

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

37 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

Related Questions

Player takes damage through collider tag 0 Answers

How to make player respawn without resetting scene 1 Answer

NullReferenceException: Object reference not set to an instance of an object 0 Answers

Screen space overlay canvas problems. 1 Answer

Fall Damage script not working 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