- Home /
How do i make a rigid body sphere, bounce of a rotating cube?
I am trying, to recreate an old game. Called "I've got some balls". But, whenever the player sphere, hits a rotating wall, i falls through it, or it just get's stuck in it. The wall is 0.4 thick. Do i have to make it thicker? The wall rotation code:
if (turnAxis == 0)
rotation = Quaternion.Euler(0, desiredAngle, 0);
else if (turnAxis == 1)
rotation = Quaternion.Euler(desiredAngle, 0, 0);
else if (turnAxis == 2)
rotation = Quaternion.Euler(0, 0, desiredAngle);
transform.rotation = rotation;
The ball is my own physics material, called Marple.
Answer by ahmedbenlakhdhar · Dec 07, 2014 at 02:51 PM
Try to use rigidbody.rotation
instead of transform.rotation
. Use it in FixedUpdate()
instead of Update()
, like this:
Quaternion rotation;
void Update () {
if (turnAxis == 0)
rotation = Quaternion.Euler(0, desiredAngle, 0);
else if (turnAxis == 1)
rotation = Quaternion.Euler(desiredAngle, 0, 0);
else if (turnAxis == 2)
rotation = Quaternion.Euler(0, 0, desiredAngle);
}
void FixedUpdate () {
rigidbody.rotation = rotation;
}
Thank you, it works a bit better now. But the sphere. Still doesnt bounce off it.
How do you move the sphere?
If you use a script to move it, then please show it.
void Update ()
{
if (transform.position.y < deathPoint.transform.position.y) {
rigidbody.is$$anonymous$$inematic = true;
transform.position = new Vector3 (10.3f, 1.55f, -2.5f);
isSpawning = true;
}
else if (isSpawning == true) {
rigidbody.is$$anonymous$$inematic = false;
isSpawning = false;
}
}
// FixedUpdate is called once per physics calculation
void FixedUpdate ()
{
float movementHori = Input.GetAxis("Horizontal");
float movementVerti = Input.GetAxis("Vertical");
Vector3 ballResitance = new Vector3(movementHori, 0.0f, movementVerti);
//rigidbody.AddForce (ballResitance, Force$$anonymous$$ode.Acceleration);
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.W))
{
moveSpeedUp = false;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
{
moveSpeedUp = true;
rigidbody.AddForce (cam.TransformDirection(Vector3.forward * fSpeed));
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
{
if (moveSpeedUp == true)
{
rigidbody.AddForce (cam.TransformDirection(Vector3.left * 5));
rigidbody.AddForce (cam.TransformDirection(Vector3.forward * 2));
}
if (moveSpeedDown == true)
{
rigidbody.AddForce (cam.TransformDirection(Vector3.right * 5));
rigidbody.AddForce (cam.TransformDirection(Vector3.back * 2));
}
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
{
rigidbody.AddForce (cam.TransformDirection(Vector3.back * fSpeed / 2));
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
{
if (moveSpeedUp == true)
{
rigidbody.AddForce (cam.TransformDirection(Vector3.left * 5));
rigidbody.AddForce (cam.TransformDirection(Vector3.forward * 2));
}
if (moveSpeedDown == true)
{
rigidbody.AddForce (cam.TransformDirection(Vector3.right * 5));
rigidbody.AddForce (cam.TransformDirection(Vector3.back * 2));
}
}
Try to modify the velocity, like changing
rigidbody.AddForce (cam.TransformDirection(Vector3.forward * fSpeed));
into
rigidbody.velocity = cam.TransformDirection(Vector3.forward * fSpeed);
I got it working, without the velocity thing. Thanks for the help! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613173434im_/https://answers.unity.com/themes/thub/images/avi.jpg)