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 Foxatethecake · Jan 03, 2017 at 01:58 PM · movementvector3characterspeed

My Character is moving fine horizontally and vertically, but moves way too fast diagonally

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour {
 
     public float moveSpeed;
 
     private Animator anim;
 
     private bool PlayerMoving;
 
     private Vector2 LastMove;
 
     private Rigidbody2D myRigidBody;
     
     // Use this for initialization
     void Start () {
         myRigidBody = GetComponent<Rigidbody2D>();
         anim = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () {
 
         PlayerMoving = false;
 
         if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f )
         {
             // transform.Translate  (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
             myRigidBody.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), myRigidBody.velocity.y) * moveSpeed;
             PlayerMoving = true;
             LastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
         }
 
 
         if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
         {
             // transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
             myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, Input.GetAxisRaw("Vertical")) * moveSpeed;
             PlayerMoving = true;
             LastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
         }
 
         if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f) 
         {
             myRigidBody.velocity = new Vector2(0f, myRigidBody.velocity.y);
                 }
 
         if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f) 
 
         {
             myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, 0f);
         }
             anim.SetFloat("moveX", Input.GetAxisRaw("Horizontal"));
         anim.SetFloat("moveY", Input.GetAxisRaw("Vertical"));
         anim.SetFloat("LastMoveX", LastMove.x);
         anim.SetFloat("LastMoveY", LastMove.y);
         anim.SetBool("PlayerMoving", PlayerMoving);
     }
 }
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 Pengocat · Jan 04, 2017 at 03:27 AM 0
Share

If you want direct control of the velocity every frame you should make the Rigidbody $$anonymous$$inematic and use Transform.Translate ins$$anonymous$$d. Otherwise you should use Rigidbody2D.AddForce in FixedUpdate. As a sidenote you should cache the Hash for the animator parameters ins$$anonymous$$d of using a string lookup every frame.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rabirland · Jan 03, 2017 at 02:37 PM

Use a temporal Vector3 to store the velocity. At the end of the Update, set the real velocity to: tmpVelocity.normalized * moveSpeed;

Comment
Add comment · Show 2 · 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 juicyz · Jan 04, 2017 at 12:19 AM 0
Share

You can do as rabirland said.
The reason why you are now moving twice as fast is because you are basically applying two forces when you move diagonally and only 1 when you strictly move horizon or vertical. Two forces are being applied because of the if statements.

avatar image Arycama · Apr 24, 2017 at 11:04 AM 0
Share

Not quite, normalizing a vector forces it's length to 1. For example, Normalize(0f, 0.00001f, 0) becomes (0, 1, 0).

What you should use ins$$anonymous$$d is Vector3.Clamp$$anonymous$$agnitude. This will clamp the length of the vector to a value (In this case, 1), but will not cause it to grow if it is very small.

avatar image
0

Answer by Arycama · Apr 24, 2017 at 11:08 AM

Make a local Vector3 variable called "velocity". instead of setting myRigidBody.velocity in the first two if statements, set the x and y values of velocity instead, and -don't- multiply it by rotation speed.

Finally before setting myRigidBody.velocity, use Vector3.ClampMagnitude to clamp the total of the vector to 1.

Here's an example, please note that I left out the other parts of the code as they are not related to the answer. Also I'm using Mathf.Abs for clarity, instead of two if statements, though this is not essential:

 void Update()
 {
     var velocity = new Vector3();
 
     if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5f)
     {
         velocity.x = Input.GetAxisRaw("Horizontal");
     }
 
     if (Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0.5f)
     {
         velocity.y = Input.GetAxisRaw("Vertical");
     }
 
     // This is where the magic happens
     velocity = Vector3.ClampMagnitude(velocity, 1) * moveSpeed;
 }
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

10 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

Related Questions

GameObject wont stop rotating ? 1 Answer

character(ball) rolls with less speed when I build it 2 Answers

Movement speed difference between the different devices 1 Answer

Can't change the speed of a character. 2 Answers

Increasing the speed of an object when the scale is decreased and vice versa 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