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 getyour411 · Jan 14, 2014 at 09:16 AM · transformtranslatestrafe

Strafe movement speed

My vertical and strafe axis are setup w/ the same paremters, except for the keys, in Input manager. This code moves me as expected on Vertical, but it goes extremely fast on Strafe, movespeed is the same. Why the turbo boost on sidemove?

 // movement code - turn left/right with Horizontal axis:
     myTransform.Rotate (0, Input.GetAxis ("Horizontal") * turnSpeed * Time.deltaTime, 0);
     // update surface normal and isGrounded:
     ray = new Ray (myTransform.position, -myNormal); // cast ray downwards
                         if (Physics.Raycast (ray, out hit)) { // use it to update myNormal and isGrounded
                                 isGrounded = hit.distance <= distGround + deltaGround;
                                 surfaceNormal = hit.normal;
                         } else {
                                 isGrounded = false;
                                 // assume usual ground normal to avoid "falling forever"
                                 surfaceNormal = Vector3.up;
                         }
         myNormal = Vector3.Lerp (myNormal, surfaceNormal, lerpSpeed * Time.deltaTime);
         // find forward direction with new myNormal:
         Vector3 myForward = Vector3.Cross (myTransform.right, myNormal);
         // align character to the new myNormal while keeping the forward direction:
         Quaternion targetRot = Quaternion.LookRotation (myForward, myNormal);
         myTransform.rotation = Quaternion.Lerp (myTransform.rotation, targetRot, lerpSpeed * Time.deltaTime);
         // move the character forth/back with Vertical axis:
 
         if(Input.GetAxis("Vertical") != 0)            
             myTransform.Translate (0, 0, Input.GetAxis ("Vertical") * moveSpeed * Time.deltaTime);
 
         if(Input.GetAxis("Strafing") != 0) {
                 myTransform.Translate (-(Input.GetAxis("Strafing")),0,0 * moveSpeed * Time.deltaTime);
             

}

Character has Rigidbody, useGravity false instead this is applied:

 rigidbody.AddForce(-gravity*rigidbody.mass*myNormal);
Comment
Add comment · Show 3
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 Tomer-Barkan · Jan 14, 2014 at 02:19 PM 0
Share

Are you sure it's actually faster and doesn't just appear faster because of the way your camera is positioned? Try measuring in units how much it actually moves over a given timespan (say one second) in vertical/strafing.

avatar image nbg_yalta · Jan 14, 2014 at 03:07 PM 0
Share

If this code is for your player controls you better should use CharacterController.$$anonymous$$ove, or rigidbody.velocity, otherwise your player will not react any colliders while moving.

avatar image getyour411 · Jan 14, 2014 at 11:04 PM 0
Share

@tbkn it's not just appearance, the x and z values are very different

@nbg_yalta definitley not CC, but maybe rigidbody; would like to understand before I refactor this bit of movement. Also, as I said originally I have a rigidboy so transform.Translate works with colliders.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by getyour411 · Jan 15, 2014 at 06:10 AM

I merged/changed it to this

         myTransform.Translate (-Input.GetAxis("Strafing") * .1f,0,Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime);


I don't understand why I had to take the Strafe Axis and reduce it sharply via that *.01; I turned on Debug.Log to see the GetAxis values and it was 1 or -1 just like Vertical, but anyway I'm able to move on.

Comment
Add comment · Show 3 · 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 Tomer-Barkan · Jan 15, 2014 at 08:18 AM 1
Share

I don't understand why it moves different values in strafe than forward, but it must be something specific to your game because I've used it hundreds of times and it worked fine. You'd probably want to find the root cause because it could cause other problems you're not even aware of (especially if this is planned to be a commercial game).

Anyway, even going with your solution of giving different speed values, I'd recommend to at least multiply by Time.deltaTime, otherwise you will have different speeds in different framerates, ie machine and load depenedent speeds.

avatar image getyour411 · Jan 15, 2014 at 07:28 PM 0
Share

Agreed, that's actually part of this line you just have to scroll out to the right to see it

avatar image Tomer-Barkan · Jan 15, 2014 at 08:43 PM 1
Share

Only the Z is multiplied by the frame time, you should multiply the X as well...

avatar image
0

Answer by RevengeOfTheFrog · May 03, 2014 at 12:57 AM

It's a parenthesis problem (It's easier to see if you format it like this): The reason why .1 probably looks like works is because it's a close enough value to moveSpeed * Time.deltaTime

What you have:

   if(Input.GetAxis("Vertical") != 0)         
     myTransform.Translate (
       0, 
       0, 
       Input.GetAxis ("Vertical") * moveSpeed * Time.deltaTime
     );
   if(Input.GetAxis("Strafing") != 0) {
     myTransform.Translate (
        -(Input.GetAxis("Strafing")),
        0,
        0 * moveSpeed * Time.deltaTime
     );

That second part should be:

   if(Input.GetAxis("Strafing") != 0) {
     myTransform.Translate (
        -(Input.GetAxis("Strafing"))  * moveSpeed * Time.deltaTime,
        0,
        0
     );
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

21 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

Related Questions

instantiate,destroy,gain speed in time 2 Answers

Moving multiple transforms from an array in a single script 0 Answers

Tranform.translate does not have constant speed 1 Answer

Match position of two object without making one a child of the other 4 Answers

Wait a fraction of a second within a Function Update() 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