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
1
Question by TakMarche · May 29, 2013 at 07:54 AM · translatetransform.translate

Removing The Slide From Transform.Translate

I'm trying to have a simple cube move across the screen vertically and horizontally but at the same time diagonally. I got all of this down with a simple transform.Translate but my problem is that I have a "slide" when I go in one direction then switch to another for example: I got up and decide to go left, I slide up slightly before I fully stop.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControls : MonoBehaviour {
 
     public GameObject player;
     public float speed = 10.0f;
     public float transH;
     public float transV;
 
 
     void Update()
     {
         PlayerProperties playerProp = GetComponent<PlayerProperties>();
 
         if(transV >= 0.001f && transH == 0)
         {
             playerProp.playerState = PlayerProperties.PlayerState.Up;
             playerProp.changePlayer = true;
         }
 
         if (transV <= -0.001f && transH == 0)
         {
             playerProp.playerState = PlayerProperties.PlayerState.Down;
             playerProp.changePlayer = true;
 
         }
 
         if (transV == 0 && transH <= -0.001f)
         {
             playerProp.playerState = PlayerProperties.PlayerState.Left;
             playerProp.changePlayer = true;
         }
 
         if (transV == 0 && transH >= 0.001f)
         {
             playerProp.playerState = PlayerProperties.PlayerState.Right;
             playerProp.changePlayer = true;
         }
 
         if (transV >= 0.001f && transH >= 0.001f)
         {
             playerProp.playerState = PlayerProperties.PlayerState.UpRight;
         }
     }
 
     void FixedUpdate()
     {
         transV = Input.GetAxis("Vertical") * speed * Time.deltaTime;
         transH = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
 
         transform.Translate(transH, transV, 0);
     }
 
 
 }
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
1
Best Answer

Answer by Lockstep · May 29, 2013 at 01:32 PM

The reason for this is Input.GetAxis. It smoothes out the input from the keys. Try Debug.Log(Input.GetAxis); in Update to see what I mean.

To avoid this you can use Input.GetAxisRaw instead. This will return 1 iff the possitive button is pressed -1 iff the negative button is pressed and 0 iff neither is pressed.

Also you shouldn't handle the input in FixedUpdate if you have the rest of your calculation in Update. Fixed Update is used for physics.

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 TakMarche · May 29, 2013 at 10:10 PM 0
Share

This works perfectly! Thanks :) and I'm also noting the FixedUpdate and Update.

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

14 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

Related Questions

Error: Method not found: 'UnityEngine.Rigidbody.Translate'. 1 Answer

instantiating transform.translate in the wrong position? 1 Answer

How to stop transform.Translate instantly ? 1 Answer

Move terrain towards object? (illusion of moving forward) 1 Answer

Custom movement problem 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