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
0
Question by DamBossik · May 09, 2020 at 01:42 PM · 2dmovementsmoothtop-down

2D smooth top-down movement

Hi. I'm new to unity. I don't know how to smotoh my top-down movement. Can someone help?

 void Update()
 {
     movement.x = Input.GetAxisRaw("Horizontal");
     movement.y = Input.GetAxisRaw("Vertical");
     mouse = cam.ScreenToWorldPoint(Input.mousePosition);
 }

 private void FixedUpdate()
 {
     rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);
     Vector2 lookDir = mouse - rb.position;
     float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
     rb.rotation = angle;
 }
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 JackB34 · May 09, 2020 at 02:34 PM 1
Share

I am also a beginner, but try changing, Input.GetAxis to Input.GetAxis. I am not sure if that will work, but try it and see. GetAxisRaw is for more snappy movement. Also, if you can't get that to work try this Brackeys tutorial: https://www.youtube.com/watch?v=whzomFgjT50

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kirbygc00 · May 09, 2020 at 05:50 PM

Some static helper function, should be in its own class c# class, but you could put it on your player if you like. We are using a plane (assuming your level is flat) to simplify detection for where the mouse is. All mouse values will contain a "height" of zero (which is what i wanted, you can change this)

   // MouseHelper.cs
     static Plane plane = new Plane(Vector3.up, 0f);
 public static Vector3 GetWorldPosition()
         {
             if (cam != null)
             {
                 Ray ray = cam.ScreenPointToRay(Input.mousePosition);
 
                 if (plane.Raycast(ray, out float distanceToPlane))
                 {
                     return ray.GetPoint(distanceToPlane);
                 }
             }
 
             return Vector3.zero;
         }

player script: We are grabbing the input in the update method and storing it in a class field. In the fixed update we use the input to change the character position. This script assumes your "Up" is Y and your forward is Z.

   // playerscripts.cs
 vector3 motion;

     public void Update()
     {
 // grabbing our motion from update because it runs more frequently
             motion = GetMovementFromInput();
         }
 
 
         public void FixedUpdate()
         {
 // applying position/rotation changes when the physics engine runs because we are using Rigidbody.ApplyForce
             UpdatePlayerRotation(motion);
             UpdatePlayerPosition(motion);
        }
 private void UpdatePlayerRotation(Vector3 motion)
         {
             // get mouse pos
             var mousePos = MouseHelper.GetWorldPosition();
             
             // lock y to unit's current y
             var lookTarget = new Vector3(mousePos.x, gameObject.transform.position.y, mousePos.z);
             
             Owner.transform.LookAt(lookTarget);
         }
 
 // i'm using the new input system... you can sub out the input.horizontal and forward for what you have now- Input.GetAxis("Horizontal")
         private Vector3 GetMovementFromInput(InputValues input)
         {
             var posX = input.Horizontal * movementSpeed * Time.deltaTime;
             var posY = 0;
             var posZ = input.Forward * movementSpeed * Time.deltaTime;
 
             var motion = new Vector3(posX, posY, posZ);
             return motion;
         }
 
         private void UpdatePlayerPosition(Vector3 motion)
         {
         rb.AddForce( motion.normalized * movementSpeed);
         }

Hope it helps =)

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

307 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 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

Unity 2D - Smooth movement over tiles?? 1 Answer

Top-down movement in 2D 1 Answer

Anchored Rotation with Gradual Movement 0 Answers

Complex 2D movement (X,Y) using velocity. 1 Answer

Smooth Character Movement 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