# Collision and Rigidbody Issues

Hello,

I don’t like posting every problem that I encounter unless I’ve exhausted all means and looked to get outsource help via videos and the Internet, however through several days of trial and error and researching issues about collisions and rigidbody’s I’m officially stumped. I have a little project that I want to create where I can move a cube by flipping it over and over going left, right, up, and down. The script I’m using does just that, but my only issue is when I get to any other gameobject the cube can be forced through by continuing to hold the arrow key in the walls direction. Is there anything that I can do to fix this very annoying problem. I’ve attached my script so people can create a very basic ground, wall , and cube that flips to see where my issue lies.

V/r,
Quincey

using System.Collections;
using UnityEngine;
public class TumblingCube : MonoBehaviour
{
public float tumblingDuration = 0.2f;
void Update()
{
var dir = Vector3.zero;
if (Input.GetKey(KeyCode.UpArrow))
dir = Vector3.left;
if (Input.GetKey(KeyCode.DownArrow))
dir = Vector3.right;
if (Input.GetKey(KeyCode.LeftArrow))
dir = Vector3.back;
if (Input.GetKey(KeyCode.RightArrow))
dir = Vector3.forward;
if (dir != Vector3.zero && !isTumbling)
{
StartCoroutine(Tumble(dir));
}
}
bool isTumbling = false;
IEnumerator Tumble(Vector3 direction)
{
isTumbling = true;
var rotAxis = Vector3.Cross(Vector3.up, direction);
var pivot = (transform.position + Vector3.down * 0.5f) + direction * 0.5f;
var startRotation = transform.rotation;
var endRotation = Quaternion.AngleAxis(90.0f, rotAxis) * startRotation;
var startPosition = transform.position;
var endPosition = transform.position + direction;
var rotSpeed = 90.0f / tumblingDuration;
var t = 0.0f;
while (t < tumblingDuration)
{
t += Time.deltaTime;
transform.RotateAround(pivot, rotAxis, rotSpeed * Time.deltaTime);
yield return null;
}
transform.rotation = endRotation;
transform.position = endPosition;
isTumbling = false;
}
}

You can’t use transform.Rotate much like you can’t use Transform.Translate or set the position of the transform if you want it to respect collisions.

You must apply forces to the rigidbody instead. use this:

Also your Coroutine should probably yield return new WaitForFixedUpdate(); as updating physics outside of the physics FixedUpdate can have strange results.

Anthony,

Thank you for the reply and API reference page I didn’t know you needed to apply forces to the rigidbody. Could i simply use the same script that I have and change certain lines of code to read…

rb.AddTorque(transform.up * torque * turn);

I’m not really sure as I don’t know the details about your game and how you want the cube to move around. But yeah replacing transform.rotate with rigidbody.AddTorque will solve your moving through walls issue.

Physics can be tricky to get right. There are a lot of edge cases that you need to test and it can often not give you as precise control over objects as you would like. Just experiment and see what happens.

Relying on physics would make your game from here on out rely on physics. Sounds obvious but what this means is every form of uncertainty comes into play. you can no-longer be certain that your block will travel in a completely straight line since rolling about could cause the cube to go off-course, or knocking into a wall forces it off-course.

If you need precision in the way your cube moves, you probably should not rely completely on physics

You can make your cube rolling completely visual using animation, and have the actual collision be done with an invisible collider which does not actually roll around. With no actual rolling around you can be at least more certain if not completely that the cube will move how you want it to. Unfortunately the gradual speedup and slowdown by using physics forces to move would make it look like your cube is sliding rather than rolling around.

You could use raycast to detect if there is something in the way of the move direction before executing your move/roll functionality. I personally think this solution works best. It does not interfere with how you are currently doing things, but it is merely a band-aid solution, and I can’t predict if this will affect later development.

The issue that I have is I have a script that makes the cube flip in the direction of the arrow keys. I have a rigidbody attached to the cube but the other gameobject which is a cube too that’s stretched into a wall. When I press the arrows to move the cube if I keep holding the arrow key in the path of the wall the cube will force its way through

That is because you are directly manipulating the transform of the cube. Wouldn’t have mattered if you had the Rigidbody on or not. What I’m trying to say is, simply switching over to using forces/torque may not solve your problem, or even create new problems, depending on what your cube has to do in the game. I suggest you explain your game so a more tailored answer can be given.