andeeee:
What I am seeing is that the “direction” values sent to SimpleMove on the desktop are small compared to the “direction” values on the iPhone. Which made me think that the time between calculating the “direction” values for SimpleMove were larger on the iPhone because the CPU was overloaded on the iPhone. So I removed all of the Finds and GetComponents from the Patrol portion of the script attached to the GameObject, and moved them to the Awake portion of that script. This sped things up a bit, but I still see the circling, which I don’t see on the desktop.
Each GameObject has a script attached to it which has the basic functionality below (which was lifted from the AI.js script in Lerp 3D FPS tutorial)
function Start(){
Patrol();
}
function Patrol(){
while(true) {
if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance) {
curWayPoint = PickNextWaypoint (curWayPoint);
}
MoveTowards(waypointPosition);
yield;
}
}
function MoveTowards (position: Vector3) {
var direction = position - transform.position;
direction.y = 0;
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
var forward = transform.TransformDirection(Vector3.forward);
var speedModifier = Vector3.Dot(forward, direction.normalized);
speedModifier = Mathf.Clamp01(speedModifier);
direction = forward * speed * speedModifier;
GetComponent (CharacterController).SimpleMove(direction);
}
}
On the desktop, the advances to the waypoint, as evidenced by the “direction” input to SimpleMove, start small and get bigger as the GameObject gets closer to the waypoint. But on the iPhone, the values of “direction” start off quite large, which causes the movement to overshoot the waypoint and oscillate and also spin.
On the desktop, when approaching a waypoint whose x position is at 340.0, the direction vector for the respective position vector are:
x-position direction
----------------------------------
344.1 (-4.2, 0, 15.3)
344.0 (-14.2, 0. 25.5)
343.5 (-24.5, 0, 29.5)
342.6 (-34.1, 0, 29.7)
341.4 (-41.7, 0, 27.6)
339.9 (-12.6, 0, -6.2)
whereas on the iPhone it looks like
x-position direction
----------------------------------
344.1 (-60.0, 0, 0.0)
324.0 ( 52.8, 0. -18.3)
333.5 ( 53.2, 0, 27.8)
344.4 (-60.0, 0, 0.0)
325.1 ( 59.0, 0, -3.3)
Once the GameObject is within a distance of 2 of the waypoint, then I pick a new waypoint. But on the iPhone, the GameObject keeps overshooting the waypoint and thus never gets a new waypoint selected, and thus circles.
A few questions:
-
The “yield” causes a break from the MoveTowards function. What causes re-entry into the MoveTowards function? Is it once all the other functions have been executed?
-
There are two Update() functions, 1) on the MainCamera that counts time before more GameObjects should be added, and 2) one that monitors when the user has the touched the screen. Should I only have one Update() function?
-
Do you have a good suggestion on how to slow down the GameObject as it approaches the waypoint?