- Home /
Making a ball roll and keeping the axis?
This is my script tried enabeling the rigidbody.AddTorgue, but it will then rotate the axis, rather wanna see the texture "roll/rotate" :)
Player is a ball that can jump and move, when moving right i want it to gain momentum.
void Update () { Vector3 horMovement = Input.GetAxis("Horizontal") transform.right Time.deltaTime * PlayerSpeed;
if (Input.GetKeyUp("space") && Physics.Raycast(transform.position, -transform.up, MaxJumps)) { rigidbody.AddRelativeForce(transform.up * PlayerJumpSpeed, ForceMode.Impulse); }
//rigidbody.AddTorque(Vector3.right * 1 *Time.deltaTime);
//move player
transform.Translate(horMovement);
}
I'm a bit confused with your question. Surely if you assigned the texture to your ball, and it rolls, the texture should be rotating as well. I don't see how the ball would roll but the texture not.
Answer by Bormeth · Apr 02, 2011 at 03:32 PM
Solved it myself :)
Vector3 movement = (Input.GetAxis("Horizontal") -Vector3.left movementSpeed) + (Input.GetAxis("Vertical") Vector3.forward movementSpeed); rigidbody.AddForce(movement, ForceMode.Force);
Did the job :)
Good job working it out on your own! One comment I have on your method -- and this really depends on the gameplay you're trying to achieve -- is that applying a directional force directly will allow the player to move around in the air (obviously fixable if needed with some checks), whereas adding torque will allow the player to roll the ball directly so it will only get momentum when its on the ground.
Answer by coastwise · Mar 31, 2011 at 10:58 AM
If your player is a ball, why not simply let it rotate? You could still make it jump upwards if you add a force (rather than a relative force) upwards like so:
void Update () { if (Input.GetKeyUp("space") && Physics.Raycast(transform.position, Vector3.down, MaxJumps)) { rigidbody.AddForce(Vector3.up * PlayerJumpSpeed, ForceMode.Impulse); }
// move player by letting them roll the ball
rigidbody.AddTorque(Vector3.right * PlayerRollSpeed * Input.GetAxis("Horizontal") * Time.deltaTime);
}
Try giving that a shot. ('PlayerRollSpeed' should probably be something like 100)
Hmm thx m8 for answering :) But dosnt seem to work now the player wont even move. Used this
if (Input.Get$$anonymous$$eyUp("space") && Physics.Raycast(transform.position, Vector3.down, $$anonymous$$axJumps))
{
rigidbody.AddForce(Vector3.up * PlayerJumpSpeed, Force$$anonymous$$ode.Impulse);
}
// move player by letting them roll the ball
rigidbody.AddTorque(Vector3.right * PlayerRollSpeed * Input.GetAxis("Horizontal") * Time.deltaTime);
Yeah, it wasn't moving for me either until I really cranked the PlayerRollSpeed variable.