- Home /
Answer by Varazdt · Jun 28, 2014 at 01:00 PM
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);
}
}
Answer by POLYGAMe · Jan 29, 2012 at 10:18 PM
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.
Or, you could use Unity's physics... I haven't touched much of that but addforce could do it... would roll the ball for you.
Thanks. But It didn't work. The ball just keeps rotating and going down forever. I want it to roll when I move it like in a terrain.
Like I said, you'd need to tweak it. $$anonymous$$ake variables for speed and rotation (ins$$anonymous$$d of using "magic numbers" and have the rotation match the speed. Then when it's not moving it won't roll.
Answer by shahar3447 · Sep 28, 2014 at 03:36 PM
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);
}
Your answer
Follow this Question
Related Questions
Making a ball roll and keeping the axis? 2 Answers
pong rolling ball 1 Answer
Rolling mable... rigid body? 1 Answer
Roll-A-Ball Problem 2 Answers
Top-Down Camera Trouble 1 Answer