• 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
Question by Jusskarr · Sep 05, 2014 at 05:36 PM · movementspeeddiagonal

Diagonal movement speed difference with normalized vector

Hi, I realize t$$anonymous$$s question pops up a lot but i can't figure out whats wrong here. I'm making my own movement controller script to make movement relative to an orbiting camera on a t$$anonymous$$rd person setup. I've fit most of it together from bits and pieces of scripts that i've looked up and it works pretty ok.

I'm using a normalized vector for direction of movement along the x-z plane, multiplied by speed, and adding vertical speed afterwards:

 Vector3 movement = moveDirection * speed + new Vector3(0, verticalSpeed, 0);
 controller.Move(movement * Time.deltaTime);


I made a little debug speedometer to make sure, w$$anonymous$$ch confirmed that indeed diagonal speed is sqrt(2)*regular speed. Debugging I also confirmed that moveDirection is normalized. Am I misinterpreting how CharacterController.Move works?

After coming to a dead end there I tried making my own character controler and started on a basic movement controller using Transform.Translate. I had no problems there, speed remained constant in every direction. I tried adding a character controller and using Move with the same input, and it still woked.

I really don't understand whats going on then. In one case speed varies, in the other it does not and both have virtually the same input. There must be somet$$anonymous$$ng Im missing here.

Here are the scripts:

The one that works

 public class BasicMovement : MonoBehaviour {

     public float speed = 6f;
     public bool useCharController = false;
 
     void Update () {
         CharacterController controller = GetComponent<CharacterController>();
         Vector3 translation = new Vector3(Input.GetAxisRaw("Horizontal"),
             0, Input.GetAxisRaw("Vertical"));
         translation.Normalize();
         translation = translation * speed * Time.deltaTime;
         if (useCharController){ 
             controller.Move(translation);
         }else{
             transform.Translate(translation);
         }
     }
 }
 

The one that doesn't

 public class MovementController : MonoBehaviour {
 
     public float speed = 6.0F;
     public float gravity = 20.0F;
 
     public float jumpSpeed = 8.0F;
     public int maxJumps = 2;
     public float jumpCooldown = 0.5f;
 
     public Transform cameraTransform;
     public Transform playerModelTransform;
 
     public float modelTurnSpeed = 2.0f;
     
     private Vector3 moveDirection = Vector3.zero;
     private float verticalSpeed = 0f;
     private int jumpCount = 0;
     private float lastJumpTime = -1f;
 
     void Update () {
         CharacterController controller = GetComponent<CharacterController>();
         bool canJump = jumpCount < maxJumps;
 
         // Forward vector relative to the camera along the x-z plane    
         Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
         forward.y = 0;
         forward = forward.normalized;
         // Right vector relative to the camera
         // Always orthogonal to the forward vector
         Vector3 right = new Vector3(forward.z, 0, -forward.x);
 
         float h = Input.GetAxisRaw("Horizontal");
         float v = Input.GetAxisRaw("Vertical");
 
         bool isMoving = Mathf.Abs(h) > 0.02f || Mathf.Abs(v) > 0.02f;
 
         // x-z plane movement
         if (controller.isGrounded) {
             moveDirection = h*right + v*forward;
             moveDirection = transform.TransformDirection(moveDirection)
                 .normalized;
             
             // turn the character model in the direction of movement
             if (isMoving) {
                 Vector3 lookForward = Quaternion.LookRotation(forward).eulerAngles;
                 lookForward.x = lookForward.z = 0;
                 playerModelTransform.rotation = Quaternion
                     .Slerp(playerModelTransform.rotation, 
                     Quaternion.Euler(lookForward), modelTurnSpeed * Time.deltaTime);
             }          
         }
 
         // y axis movement
         if (canJump && Input.GetButton("Jump")) {
             if (lastJumpTime + jumpCooldown < Time.time) {
                 lastJumpTime = Time.time;
                 verticalSpeed = jumpSpeed;
                 jumpCount++;
             }
         }
 
         if (controller.isGrounded && !Input.GetButton("Jump")){
             jumpCount = 0;
             verticalSpeed = 0;
         } else {
             verticalSpeed -= gravity * Time.deltaTime;
         }
         Vector3 movement = moveDirection * speed + new Vector3(0, verticalSpeed, 0);
 
         controller.Move(movement * Time.deltaTime);
     }
 }
 

The debug speedometer

 public class DebugSpeedometer : MonoBehaviour {
 
     public float timeStep = 1f;
     public float currentSpeed = 0f;
     private float lastMeasurement = -1f;
     private Vector3 lastMeasurementPosition;
 
     void Start () {
         lastMeasurementPosition = transform.position;
     }
     
     void Update () {
         if (lastMeasurement + timeStep < Time.time) {
             lastMeasurement = Time.time;
             currentSpeed = Vector3.Distance(transform.position, lastMeasurementPosition) / timeStep;
             lastMeasurementPosition = transform.position;
         }
     }
 }





Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Increased strafe speed\ 1 Answer

Change int value for 30 sec... 1 Answer

Diagonal moving is faster .Help in solving a bug! 1 Answer

Curvy Spline Tool (Controlling Speed) 2 Answers

How to move an object back/forth on a set path? 2 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