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
1
Question by bspin · Mar 28, 2015 at 05:01 PM · 2dmovementtopdown

2D Topdown movement without sliding

I'm currently prototyping a game that is 2D topdown but i have trouble with my movement script. I watched the Live Training: Top Down 2D Games video (and allot more video's) that got me close but i still having problems with the accel and deceleration. I dont have a ship or anything just a person walking so i want him to stop moving as soon as i release my walking keys (WASD). You could say its the same system as Hotline Miami where you only use the mouse for the direction and attacking.

This is my current code that works but doesn't stop the character right away, it keeps sliding. So my question would be, how to remove the sliding part?

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     
     public float speed = 10;
     
     void FixedUpdate()
     {
         ControlMouse();
     }
 
     void ControlMouse() 
     {
         // capture mouse position. We need to convert between pixels and World Unities
         var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         // Basicly it's looking to the mouse position. And rotating.
         Quaternion rot = Quaternion.LookRotation (transform.position - mousePosition, Vector3.forward);
         
         // LOOKING AT MOUSE
         // set our gameobject rotation to the calculated one rotation
         transform.rotation = rot;
         // doesnt changerotation angles for x, y.
         transform.eulerAngles = new Vector3 (0, 0, transform.eulerAngles.z);
         // prevents from "slide"
         GetComponent<Rigidbody2D>().angularVelocity = 0;
         
         // MOVEMENT
         // this because moving foward means moving in the Z axis, but this is a topdown game!
         // obs.: we still need to press up/down arrows to move!
         float input = Input.GetAxis ("Vertical"); //Input.GetAxis ("Mouse X");
         
         GetComponent<Rigidbody2D>().AddForce (gameObject.transform.up * speed  * input );
     }
 }


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 Salmjak · Mar 07, 2016 at 12:22 PM 0
Share

@bspin Have you tried just setting the Velocity to 0 when you release the button? As in Vector2 zeroVel = new Vector2(0,0); Rigidbody2D.Velocity = zeroVel;

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by thecox801 · Mar 31, 2015 at 03:31 PM

I'm actually working on a very similar mechanic for my game, that approximates the Hotline Miami movement (I even used the same video as a start). I believe the part you might have missed from the video was increasing the Linear Drag option on the player's RigidBody2D component. With a speed of 40, he set the linear drag to 5 to lessen the sliding effect.

alt text

For my own game, I used a higher linear drag to almost completely eliminate sliding. Keep in mind that you will have to increase speed in conjunction with drag. Play around with these until you find a combination that works best. Good luck!


rigidbody2d-17a6xk0.jpg (57.7 kB)
Comment
Add comment · Show 1 · 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 bspin · Mar 31, 2015 at 07:43 PM 0
Share

Thanks ill try some different value's.

For now i've just removed the Rigidbody and use the normal position changes.

avatar image
2

Answer by FooTaylor · Mar 07, 2016 at 11:12 AM

Hi, you could set the gravity parameter for the input keys on Edit>Project Settings>Input.

For example, if I increase gravity for horizontal inputs, left and right movements will deaccelerate faster, and you won't need to change anything to linear drag and speed.

alt text

alt text

Bye!


inputgravity1.png (22.7 kB)
inputgravity2.png (27.4 kB)
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

Character movement problem, floats away 1 Answer

2D Directional top down movement,Topdown 2d Directional Movement 0 Answers

How do I make a 2D object face the direction it is moving in top down space shooter 1 Answer

(2D Movement) How do I make my sprite move up, down, left, and right, without moving diagonal? 1 Answer

Top-down movement in 2D 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