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 shmeeb · Jun 28, 2016 at 11:27 AM · movement2d-platformer

Running script isn't working correctly

Newbie here, I'm working on a running script for a 2D platformer but I can't get it to work. Here's what I've got so far:

 using UnityEngine;
 using System.Collections;
 
 public class nuntiControllerScript : MonoBehaviour {
     public float maxSpeed = 10f;
     bool facingRight = true;
     public float move;
     Animator anim;
 
     // Use this for initialization
     void Start()
     {
 
         //The animator is called in order to play a running animation.
 
         anim = GetComponent<Animator>();
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
 
         //While the D key is held down, move is 1
 
         while (Input.GetKey(KeyCode.D))
         {
             move = 1;
         };
 
         //while the A key is held down, move is -1
 
         while (Input.GetKey(KeyCode.A))
         {
             move = -1;
         };
 
         //The value of the speed parameter is altered to be equal to the value of move. This causes the idle animation to transition into running.
 
         anim.SetFloat("Speed", Mathf.Abs(move));
 
         //The velocity of the character is 10 x move, which is either 1, so the character moves to the right, or -1 so he moves to the left
 
         GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
 
         if (move > 0 && !facingRight)      //If the move is greater than 0 and you aren't facing right,
             Flip();                        //turn around so you are.
         else if (move < 0 && facingRight)  //If the move is less than 0 and you are facing right,
         {                                  //turn around to face left and set facingRight to false.
             Flip();
             facingRight = false;
         }
             
     }
 
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

Whenever I try and run the code as it is now, the program crashes. I've also had issues where:

  • the sprite would move, albeit only to the left, but there'd be huge lag and the sprite wouldn't flip

  • the sprite would just rotate slightly to the right when the right arrrow key was pressed

  • the sprite just wouldn't move (This was the most common problem)

I've been trying to make this work for a couple of days now. Anyone got any advice?

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
1
Best Answer

Answer by tanoshimi · Jun 28, 2016 at 11:31 AM

Don't ever use while() in Update() - you'll hold up your entire execution thread by going round and round in that loop until it's no longer true (in your case, for as long as you're holding down the A or D keys).

I suspect you meant:

      if(Input.GetKey(KeyCode.D))
      {
          move = 1;
      };
 
      //while the A key is held down, move is -1
 
      if(Input.GetKey(KeyCode.A))
      {
          move = -1;
      };

However, note that even then you shouldn't really check for input inside FixedUpdate - fixed update is intended for physics updates or other events that need to happen on a regular, fixed timestep - listening for input etc. that needs to be done in every frame should be in Update(). You might want to look through some of the tutorials at https://unity3d.com/learn/tutorials/topics/2d-game-creation

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity2d - Player Movement. Character "jumps" when transition from idle > run 0 Answers

Please help with infinite gravity swapping problem 0 Answers

2D Movement System (Stop Movement on Collision) 1 Answer

Kinematic 2D Rigidbody movement: Rigidbody2D.MovePosition vs Rigidbody2D.position problem 1 Answer

Best way to move back and forth between horizontal planes using doors? 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