Hi guys,
I am making a game where the main player is a ball and I want to make the ball roll when I move it. How can I make that? What script should I write?
Thanks.
Hi guys,
I am making a game where the main player is a ball and I want to make the ball roll when I move it. How can I make that? What script should I write?
Thanks.
The right way to roll the ball it is roll ball to the way with your camera will faced…So for this I wrote the code on c# witch will move the ball always in right way,the way witch look your camera…Sooo
public float ballSpeed = 10.0f;
void FixedUpdate(){
if(input.GetKeyDown(KeyCode.W)){
this.rigidbody.AddForce(Camera.main.transform.forward * ballSpeed);
}
if(input.GetKeyDown(KeyCode.S)){
this.rigidbody.AddForce(-Camera.main.transform.forward * ballSpeed);
}
if(input.GetKeyDown(KeyCode.A)){
this.rigidbody.AddForce(-Camera.main.transform.right * ballSpeed);
}
if(input.GetKeyDown(KeyCode.D)){
this.rigidbody.AddForce(Camera.main.transform.right * ballSpeed);
}
}
A simple way would be to use transform.Translate as well as transform.Rotate, so that the ball rotated as it moved:
// Moves ball along the X and rotates on the z to simulate rolling
transform.Translate(5, 0, 0);
transform.Rotate(0, 0, 5);
Obviously that’s REALLY basic and you’d need to tweak the math but shouldn’t be too hard to get going.
add to the sphere a rigid body and use this code:
public float speed;
void FixedUpdate (){
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement * speed * Time.deltaTime);
}