- Home /
Make a cube bounce off four walls
Hi,
I finally got the cube moving within a confined area but after colliding with two walls in quick sucession it breaks through - as seen here at 21- seconds : http://screencast.com/t/NjczNzYxMWQt
I was told the best way to avoid this would be to start again and use physics but the problem is im not too sure where to start and I dont want to recreate that mistake.?
The code currently used is:
var speed : int = 20;
function Awake() { //print ("no collision as of yet"); }
function Update () {
transform.position += transform.forward * Time.deltaTime * speed;
}
function OnTriggerEnter (hit : Collider) { var rot = Quaternion.FromToRotation(Vector3.forward, hit.transform.up); transform.rotation = rot; transform.rotation.eulerAngles += Vector3(0, Random.Range(-90,90), 0); }
It only seems to work with planes as the walls , if I try cubes or box collides the cube object seems to fly up instead of doing any bouncing? Thanks
Answer by Tetrad · Jun 26, 2010 at 12:55 AM
The answer to your question would be pretty much identical to the one I wrote for another question recently, so I'll just link to it:
http://answers.unity3d.com/questions/13402/getting-maze-walls-using-3d-meshes-to-work/13418#13418
Also, if your cube has a rigidbody, you should be doing movements inside FixedUpdate
instead of Update
Try this link, for Tetrad's suggestion: http://answers.unity3d.com/questions/20502/getting-maze-walls-using-3d-meshes-to-work.html
Your answer
Follow this Question
Related Questions
How to move cubes with finger 2 Answers
Make objects easier to select 1 Answer
Can I stop a lerp completely when it gets to 20% of its original value? 3 Answers
How to 'flick' objects in a 2d game 1 Answer
Vector3 and power 1 Answer