Hello, I have some questions about moving objects:
1: I want to have an object just continue to move until it hits a wall, bounce of and just keep going. I have tried using this script on a rigid body, with turning of gravity and rotation but eventually it will stop(and if I set the function to update, the force is only in one direction). How can I can accomplish what I want, I just want something to bounce around?
function Start() {
rigidbody.velocity = Vector3(10,0,10);
}
2: Is there any way to cause an object to move away from another object when the second object comes into a certain distance of the first object?
var speed : float = 10;
function Start() { rigidbody.velocity = Vector3(10,0,10); }
function FixedUpdate() {
rigidbody.velocity = rigidbody.velocity.normalized * speed;
}
And yes, you can detect objects by distance if they have a specific component or you can use trigger zones.
For example, create a “Repel” class and attach it to every object that should repel other Repel objects -
var distance : float;
var repelForce : float;
static var allRepel : Array = new Array();
function Start() { allRepel.Add(this); }
function FixedUpdate() {
for ( var r : Repel in allRepel ) {
var d : Vector3 = r.transform.position - transform.position;
if ( rigidbody d.magnitude < distance ) {
rigidbody.velocity += (distance - d.magnitude)/distance * d.normalized * r.repelForce;
}
}
}