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 /
avatar image
0
Question by sir_ver · Dec 13, 2018 at 11:20 AM · movementtransforminputplayermove

Problems with the movement of the player

Hey I have this code for moving the player:

 void FixedUpdate () {
          var x = Input.GetAxis("Horizontal") * Time.deltaTime * 250.0f;
          var z = Input.GetAxis("Vertical") * Time.deltaTime * 6.0f;
 
          transform.Rotate(0, x, 0);
          transform.Translate(0, 0, z);
     }

But it's not working correctly. If move the player towards a wall, the player bounces smoothly to the opposite direction even if I don't press any key. Do you have a solution for this problem?

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

Answer by AaronBacon · Dec 13, 2018 at 01:34 PM

Try using RigidBodys Velocity Component instead of transform.Translate. Im assuming you already have a RigidBody Component on your object, so try:

 public class PlayerController : MonoBehaviour
 {
     private Rigidbody rb; 
     void Awake()
     {
         rb = GetComponent<Rigidbody>(); // Connects the rb variable to the Players RigidBody Component
     }
    void FixedUpdate()
    {
    var x = Input.GetAxis("Horizontal") * Time.deltaTime * 250.0f;
    rb.velocity = new Vector3(x, rb.velocity.y,rb.velocity.z); // Set the horizontal velocity to the "x" input value (as you've assigned above) and leave the other values as they are (at their current velocity)
 
    }
 }

I normally work in 2D, so i may have got the Axis wrong there, but I believe that should work,Try using RigidBodys velocity component instead of transform.Translate() I'm assuming your Player object already has a RigidBody Component, but if not, add one and use this. eg.

 private Rigidbody rb;
 void Awake()
     {
         rb = GetComponent<Rigidbody>(); // Connects the rb variable to the Players RigidBody Component
     }
 void FixedUpdate()
    {
     rb.velocity = new Vector3(x, rb.velocity.y,rb.velocity.z);
     // Set the player's x velocity to the Horizontal input of the player (using "x" as the input variable as in your code)
     }

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 sir_ver · Dec 16, 2018 at 10:56 AM 0
Share

Ok, I'll try it out

avatar image sir_ver · Dec 16, 2018 at 11:12 AM 0
Share

i tried it with this code:
var x = Input.GetAxis("Horizontal") Time.deltaTime 250.0f; rb.velocity = new Vector3(x, rb.velocity.y, rb.velocity.z);

But it's not working properly. The player is just moving up- and downwards and it's not rotating.

avatar image Lentaq sir_ver · Dec 16, 2018 at 12:27 PM 0
Share

You can't use velocity for rotation like that. Use velocity in your Z axis for moving forward based on your Input.GetAxis("Vertical"). You can't do it with your X though. Velocity is strictly for your movement.

You should get your movement inputs in Update, not FixedUpdate, also.

If you want to rotate with Rigidbody, it has to be done with other methods. You can do it several ways. Here's the main one you probably need.

Rigidbody.$$anonymous$$oveRotation is one way. Where m_EulerAngleVelocity would be your 'x' or Input.GetAxis("Horizontal") input ins$$anonymous$$d that you get in Update.

 void FixedUpdate()
     {
         Quaternion deltaRotation = Quaternion.Euler(0, x, 0);
         rb.$$anonymous$$oveRotation(rb.rotation * deltaRotation);
     }


Or, you can use rotation directly. Rigidbody.rotation -- As it says in the descriptions, this one is instant, so there's no smoothness to it, however. So, you probably want the other.

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

189 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 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 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 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 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 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 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 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 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

Stop Object From Moving After Key Release 2 Answers

make player move in direction it's facing 2 Answers

[SOLVED] Object keeps sliding? 2 Answers

How do you move a character using a slider? 1 Answer

Player(Squre) move along fixed line (which can be straight , incline, circuler etc) with inputs of joystick ! 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