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 /
  • Help Room /
avatar image
1
Question by The-Evster · May 31, 2016 at 05:24 AM · movementphysicsplayerspeed

How to get rid of double movement speed?

when i press the 'w' key my player moves at the right speed but when i press 'w' and 'a' or 'd' at the same time my play runs much faster

My Script: using UnityEngine; using System.Collections;

public class PlayerMovement2 : MonoBehaviour {

 public GameObject Camera_Point;
 public float movespeed = 5.0f;
 public float MouseXSensitivity = 5f;
 public float MouseYSensitivity = 3f;
 public GameObject _player;

 float verticalRotation = 0;
 public float upDownRange = 60.0f;
 public float verticalVelocity = 5f;
 public float jumpSpeed = 5.0f;

 CharacterController characterController;
 Animator anim;

 void Start ()
 {
     anim = GetComponent<Animator>();
     characterController = GetComponent<CharacterController>();
     _player = gameObject;
 }
 

 void Update ()
 {
     PlayerMovement();
     Animation();
 }

 void Animation()
 {
     anim.SetFloat("InputZ", Input.GetAxis("Vertical"));
     anim.SetFloat("InputX", Input.GetAxis("Horizontal"));
 }

 void PlayerMovement()
 {
     float rotLeftRight = Input.GetAxis("Mouse X") * MouseXSensitivity;
     transform.Rotate(0, rotLeftRight, 0);


     verticalRotation -= Input.GetAxis("Mouse Y") * MouseYSensitivity;
     verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
     Camera_Point.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);

     //Player Movement
     movespeed

     float forwardSpeed = Input.GetAxis("Vertical") * movespeed;
     float sideSpeed = Input.GetAxis("Horizontal") * movespeed;

     verticalVelocity += Physics.gravity.y * Time.deltaTime;

     if (characterController.isGrounded && Input.GetButton("Jump"))
     {
         verticalVelocity = jumpSpeed;
     }

     Vector3 speed = new Vector3(sideSpeed, verticalVelocity, forwardSpeed);

     speed = transform.rotation * speed;

     //Running
     if (Input.GetKey(KeyCode.LeftShift))
     {
         movespeed = 15;
     }
     else
     {
         movespeed = 5;
     }

     characterController.Move(speed * Time.deltaTime);
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by Kamil1064 · May 31, 2016 at 06:23 AM

Hi @The Evster, that's happening because you get an velocity in forward and right in the same time. alt text Here is little example how to set up this:

 float xMov = Input.GetAxisRaw("Vertical");
 float zMov = Input.GetAxisRaw("Horizontal");
 
 Vector3 mHorizontal = transform.right * xMov;
 Vector3 mVertical = transform.forward * -zMov;
 
 Vector3 velocity = (mHorizontal + mVertical).normalized * moveSpeed;

Just use last variable to move your character.


unity-moving-too-fast-001.jpg (14.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 Bunny83 · May 31, 2016 at 12:50 PM 1
Share

That's not really a good approach since no matter how "strong" an input is you always run at max speed due to normalization.

It's easier to use Vector.Clamp$$anonymous$$agnitude. It only restricts the magnitude of the vector if it's greater than the given magnitude.

For this you should first combine all lateral movement into one vector and then clamp the result. Finally add the verticalVelocity.

So specifically just do:

  Vector3 speed = new Vector3(sideSpeed, 0, forwardSpeed);
  speed = Vector3.Clamp$$anonymous$$agnitude(speed, movespeed);
  speed.y = verticalVelocity;
  speed = transform.rotation * speed;

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Player Movement Not Always Responding 1 Answer

Jumping and collision issue 1 Answer

How do I make a surface move a player forward? 1 Answer

How to make my player move forward when on an object 1 Answer

[Help] Player stops moving when hitting wall diagonally 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