- Home /
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?
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)
}
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.
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
Follow this Question
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