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 /
avatar image
1
Question by CashGoblin · Aug 24, 2016 at 09:27 AM · movement scripttopdown

2D Top Down Movement

I've just stared to make a 2D top down game. But I cant get past the movement.

The first thing I tried was transform.translate, but the collisions are bouncy and the over all movement is choppy.

The second thing I tried was addforce, which is a lot smoother and collisions are fine, but the movement is all drifty, when you let go of the key, you keep moving. You start off slow and keep accelerating. No matter how much I change the speed, and the friction, it isn't how i want it.

Is there any 2D top down moving script that makes my player move at a constant speed until I let go of the button in which the player will stop immediately?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Zoogyburger · Aug 25, 2016 at 02:34 AM

Here's a very simple 2d movement script:

 using UnityEngine;
 using System.Collections;
 
 // PlayerScript requires the GameObject to have a Rigidbody2D component
 
 [RequireComponent (typeof (Rigidbody2D))]
 
 public class PlayerScript : MonoBehaviour {
     
 
     public float playerSpeed = 4f;
 
     void Start () {
     
     }
     
     void FixedUpdate () {
         Vector2 targetVelocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
         GetComponent<Rigidbody2D>().velocity=targetVelocity * playerSpeed;
     }
 }

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
1

Answer by Pleroma · Aug 25, 2016 at 06:09 AM

I'm presently developing a 2D top down game as well, and I ran into a similar problem. Setting your Velocity has been much more fruitful for me than adding force, as @Zoogyburger suggested. However, if that's still too floaty for you, you might want to do what I did, and use GetAxisRaw instead of GetAxis.

GetAxis returns a float between -1 and 1, which depending on the speed of the transition from 0 to 1 and back again, might leave your character moving a little even after you've released the key. I understand unity has some very interesting tools to tune how quickly the GetAxis float changes, but for my game (and possibly yours as well), it was overkill. GetAxisRaw returns either -1, 0, or 1. So when you release the key, the GetAxis value immediately drops to 0 -- the character stops.

If GetAxisRaw is too snappy for your game, I would suggest looking at the Input Manager in the Unity API. That should have all the tools you need.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

3D TopDown Shooter Movement Script 1 Answer

2D Topdown Character Contoller 0 Answers

Add a Charger/Cooldown system to a movement script for a 2D Top-Down Shooter. 1 Answer

How to animate car movement without player input \ 2D TOP DOWN GAME 1 Answer

Movement keeps doubling when using militple input 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