- Home /
How do I make a Game Object move when I collide it ingame.
I've looked but obviously don't know the words to find if this question has been asked before I spawn a game object in front of my controller with a click and when I bump into it nothing. I would like so that when I collide with this box or object that it can... well knocked about but I don't know the how having been scripting a whole week.
Please answer with some kind javascript with instructions.
There are other factors to take into account here, Are you using a characterControler or rigidbody for the object you are using to collide with. If a character controller you will have to program the physics into it.
Answer by MousePods · Dec 20, 2012 at 04:25 PM
This is from the Unity Script reference in the docs:
Note: Make sure the GameObject you are trying to push has a rigidbody
// this script pushes all rigidbodies that the character touches
var pushPower = 2.0;
function OnControllerColliderHit (hit : ControllerColliderHit)
{
var body : Rigidbody = hit.collider.attachedRigidbody;
// no rigidbody
if (body == null || body.isKinematic)
return;
// We dont want to push objects below us
if (hit.moveDirection.y < -0.3)
return;
// Calculate push direction from move direction,
// we only push objects to the sides never up and down
var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
// If you know how fast your character is trying to move,
// then you can also multiply the push velocity by that.
// Apply the push
body.velocity = pushDir * pushPower;
}
Answer by Morazain · Dec 21, 2012 at 12:54 AM
Thank you for the speedy reply I have been stuck on this problem since lunch. I'll try it out and I'll see what I get.
Your answer
Follow this Question
Related Questions
How do I make a realistic jump? 1 Answer
objects in the same array wont move 0 Answers
Object "crawling" over floor, how? 2 Answers
Moving Gameobject and Reseting Issues 3 Answers