- Home /
How do I create local space movecontrols for a rigid body
Hi!
I'm trying to create some controls for a rigid body character in my game. My problem is to move the character along local axis which is needed in my case. Using character controller unfortunately is not an option in my game.
I'm not very good at scripting but here is what I got so far:
function FixedUpdate ()
{
if (grounded)
{
// Calculate how fast we should be moving
var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
targetVelocity = transform.TransformDirection(targetVelocity);
targetVelocity *= speed;
// Movement in local space
var locVel = transform.InverseTransformDirection(rigidbody.velocity);
locVel.x = speed;
rigidbody.velocity = transform.TransformDirection(locVel);
}
}
I realize that my problem in this case is the locVel.x=speed, which just moves the character constantly along the x-axis, but I can't seem to figure out how to control the character with the left and right arrows.
After calculating targetVelocity you havn't used that anywhere in your rigidbody.velocity
Your answer
Follow this Question
Related Questions
Player movement controls too slow 1 Answer
How to drag and drop a instantiate prefab on my mobile game 3D using touchs! 0 Answers
Player is Speeding up in collisions 0 Answers
Movement using rigidbody.velocity to apply a constant force until stop 1 Answer
How to make brownian motion of a RigidBody animal AI? 1 Answer