- Home /
Question by
Masterdragon · Sep 13, 2012 at 05:34 PM ·
movementcollidertransformobject
How can I move an object physically correct around an other object?
Hi guys,
I wanted to move a gameobject to a location, the problem is, that with the code I found my object moves through other objects like walls..
I tried to add rigidbodies to them with no success.
If somebody got a good idea how to avoid this ghostlike behavior, tell me please ;)
My code looks like this:
var endPoint : Vector3;
var duration : float = 1.0;
var mychamp : GameObject;
private var startPoint : Vector3;
private var startTime : float;
function Start() {
startPoint = mychamp.transform.position;
startTime = Time.time;
endPoint = Vector3(12,1.5,6);
}
function Update () {
mychamp.transform.position = Vector3.Lerp(startPoint, endPoint, (Time.time - startTime) / duration);
}
Thanks in advance.
Comment
Answer by Cool Dave · Sep 13, 2012 at 07:10 PM
Unless you have Unity Pro, you probably want a bunch of waypoints that the object can follow to the destination.
var waypoints : Transform[];
var loop : boolean = false;
var targetWaypoint : int;
var waypointRadius = 0.00;
var angularDistance = 0.00;
var carCenter : Transform;
function FixedUpdate ()
{
target = waypoints[targetWaypoint];
xDistance = target.position.x - carCenter.position.x;
zDistance = target.position.z - carCenter.position.z;
angularDistance = (Mathf.Sqrt(xDistance * xDistance) + (zDistance * zDistance));
if (angularDistance < waypointRadius){
targetWaypoint += 1;
}
if (loop == true){
if(targetWaypoint >= waypoints.Length){
targetWaypoint = 0;
}
}
var angle : float = Mathf.Atan2(xDistance, zDistance) * Mathf.Rad2Deg;
}
Move your object at that angle. Hope this works. If it works and helps, please mark this answer correct.