• 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 LPCurse · May 09 at 02:17 AM · bugmovement script2d-physicscharacter controller

Character moves on its own

I am new to Unity and programming. While I think I'm getting pretty good at programming, I still can't code basic things without a tutorial. I have been making a 2D top-down shooter like Enter the Gungeon and I've used Brackey's top-down shooter tutorial to implement character movement. While it has worked reliably on my other projects, I've encountered a bug that hasn't happened to me before. The character's x and y values increase slightly even without input, causing them to move toward the top-right corner. I'm not sure how to fix it, but I have determined that it is because of the code. I think it might have something to do with the new input system, as that is the only difference from the last few times I used it.

 [SerializeField] private float moveSpeed = 15f;

 [SerializeField] private Rigidbody2D rb;

 Vector2 movement;

 public InputAction Controls;


 private void OnEnable()
 {
     Controls.Enable();
 }
 private void OnDisable()
 {
     Controls.Disable();
 }

 // Update is called once per frame
 void Update()
 {
     movement = Controls.ReadValue<Vector2>();
 }

 private void FixedUpdate()
 {
     rb.velocity = new Vector2(movement.x * moveSpeed + Time.deltaTime, movement.y * moveSpeed + Time.deltaTime);
 }

}

Thank you for your help!

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 barry1251 · May 09 at 06:55 AM 0
Share

Play some rounds in training mode and see if it happens. It’s possible you have two buttons programmed to the same action.

My-Estub.com Login

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LPCurse · May 09 at 04:38 AM

I found a solution now. While it isn't perfect, it at least causes the player to be stationary. I found out that all I really had to do was multiply Time.deltaTime instead of adding it. This causes the character to move extremely slowly, but they don't move on their own. It's not perfect, but increasing walking speed to a really high number is better than watching the character slowly drift out of view.

Here's what it looked like before:

rb.velocity = new Vector2(movement.x x moveSpeed + Time.deltaTime, movement.y x moveSpeed + Time.deltaTime);

Here's what it looks like now: rb.velocity = new Vector2(movement.x x moveSpeed x Time.deltaTime, movement.y x moveSpeed x Time.deltaTime);

Change the x to a star, I didn't know they italicize.

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

171 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

Related Questions

Character controller air control 0 Answers

How to make a 3rd person character controller 0 Answers

Moving player to the opposite direction of the mouse position 1 Answer

How to make the player move a fixed distance? (Tile per tile) 0 Answers

Character floats upward when looking up and down 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