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
0
Question by kevinv321 · Jul 13, 2013 at 09:58 AM · movementplayer

My obj moves faster when z and x movement

Hi All,

I have a basic movement script which looks like:

 h = Input.GetAxis("Horizontal");
 v = Input.GetAxis("Vertical");
 Vector3 v3_player_movement = new Vector3(h * movementSpeed * Time.deltaTime, 0f, v * movementSpeed * Time.deltaTime);
 transform.Translate(v3_player_movement, Space.World);

When I press 1 key for movement, its looks very good, but when I press 2 buttons, like up and left. The movement of my cube seems faster (more distance each second), I think this is because up + left movement will cause in a diagonal movement which you will do more distance in each second. Unfortunately I have no idea how to limit the speed so that up + left has the same distance each second as up or left single pressed?

UPDATE FULL CODE:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
     public float turnSmoothing = 15f;
     public float movementSpeed = 0.5f;
     
     private float debug_time = 0f;
     private Vector3 last_spotted;
     private float h;
     private float v;
     
     void Update() {
         h = Input.GetAxis("Horizontal");
         v = Input.GetAxis("Vertical");
         MovementManagement(h, v);
     }
     
     void MovementManagement(float h, float v) {
         if (h != 0f || v != 0f) {
           Rotating(h, v);
           Move(h, v);
         }
     }
     
     void Move(float h, float v) {
         // Calculate the direction
         Vector3 direction = new Vector3(h, 0f, v).normalized;
         // Calculate velocity
         Vector3 velocity = direction * movementSpeed * Time.deltaTime;
         // apply
         transform.Translate(velocity, Space.World);
     }
     
     void Rotating(float h, float v) {
         Vector3 targetDirection = new Vector3(h, 0f, v);
         Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
         Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
         rigidbody.MoveRotation(newRotation);
     }
 }



Thanks, all help is welcome.

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
0
Best Answer

Answer by Benproductions1 · Jul 13, 2013 at 10:12 AM

Hello,

What you are doing for movement, is you are separating movement into two axis(s), so it's easier to apply the separated input.
If you want to only move in one direction at a specified speed (and only at that speed), instead you should calculate the direction (as a vector) and them multiply it by the speed:

 float movementSpeed = 0.5f;
 
 //Get input
 float hor = Input.GetAxis("Horizontal");
 float vert = Input.GetAxis("Vertical");
 
 //Calculate the direction
 //If you are unsure what "normalized" does
 //I suggest learning how vectors work
 Vector3 direction = new Vector3(hor, 0f, vert);

 //only move when we don't have a very small Input value
 if (direction.magnitude > 0.001) {
     direction.Normalize()
 }
 else {
     return;
 }
 
 //Calculate Velocity from direction
 //And move
 Vector3 velocity = direction * movementSpeed * Time.deltaTime;
 transform.Translate(velocity, Space.World);

The code is untested, but should work. (I made sure to try to keep it #pragma strict)

Hope it helps,
Benproductions1

Comment
Add comment · Show 5 · 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 kevinv321 · Jul 13, 2013 at 05:34 PM 0
Share

@Benproductions1 Thanks a lot, it works but now my cube looks very buggy, when I stop moving, the cube sometimes slide an abnormal distance and rotates also. Any ideas what I do wrong? I posted my full code above.

avatar image kevinv321 · Jul 13, 2013 at 05:43 PM 0
Share

@Benproductions1 btw, I use a cube as gameobject for the movement :) just so you know.

avatar image Benproductions1 · Jul 14, 2013 at 01:33 AM 0
Share

You might want to check if the magnitude of the non-normalized direction > some arbitrarily small value. I'll edit my answer to include that ;)

avatar image kevinv321 · Jul 14, 2013 at 05:07 PM 0
Share

@Benproductions1 thanks again for helping me out, unfortunately it doesn't seem to be working, still a bit buggy, could you test the code if it works for you? Would be awesome, since I really cant get it to work without part that the cube moves much longer than it should.

avatar image Benproductions1 · Jul 15, 2013 at 12:15 AM 0
Share

@kevinv321 Just FIY, rotation isn't affected by my script. I just tested it in an empty project and it works perfectly.

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Moving your player to another location onTrigger 0 Answers

Should character direction come from key press, or third person controller vector3? 0 Answers

How do I set up my players controller script (How do I change the controls used to move and look) 2 Answers

Problem with player movements 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