So I’ve been trying to figure out this problem all day, I’ve figured its time for an expert to take a look. I guess it should be pretty simple for someone who knows what they’re doing.
Project description: A basic pathing system where the user clicks the screen to setup waypoints. A cube will then move to each of these waypoints, one after the other. This is a 2D view.
The problem: I’ve been trying to get the cube to rotate towards the next waypoint in line, so it looks like its “walking” towards the next waypoint. However, my rotation script has been messing up the rotation order and I can’t figure out why. When there’s more than 2 plotted points, the cube rotation will jump to the next next point’s rotation, rather than the upcoming rotation.
Instructions to replicate:
• Download my project here: http://bit.ly/kotL2R
• Run the scene > Click two points on the screen and press “z” to make the cube move. Notice how the cube faces towards the 2nd point nicely?
• Try it now with 3 points or more. The cube is jumping ahead of itself and rotating towards Point C when it should be facing Point B.
• I suspect maybe a yield or yield WaitForSeconds might have to do with it? Not sure what to try anymore.
• Tip: its best to use sharp angle paths to see how it works. If you go diagonal from A to B and then straight up from B to C, you’ll see it easily. If you go diagonal to diagonal, A down to B up to C, there will be the slight illusion that its actually working because the angles are mirror and the cube is deceptive.
Please let me know if you can tinker a solution, would appreciate the help! Thanks
I’d guess the problem lies in that you’ll instantiate the waypoint on click, and tell your object to rotate towards it (lerp on the rotation?). The problem you’ve got is that it’ll rotate to any of the waypoints you put on the screen unless you tell it specifically to rotate to the first one while that’s active, then the second etc. If you can post the code you use to rotate the cube, it’ll likely be solvable.
Hey guys, I’m going to paste the code here rather than have you guys download it. Maybe it’ll help All you really need is a cube transform and some prefab loaded into marker. Still waiting on anyone to take a crack at it. Thanks!
/*
Pathing Script
Written by Cory Liu
July 1st, 2011
Instructions: Click on the screen to plot markers. Press "z" to run. The cube will follow the path.
*/
import System.Collections.Generic;
#pragma strict
var cube : Transform; //the object that will move around
var marker : GameObject; //the markers for waypoints
var markerList = List.<GameObject>(); //a list (array) containing the marker game objects
var lineRenderer : LineRenderer; //a line renderer used for drawing lines between markers
function Update () {
//Sets the lineRenderer to draw lines from marker tomarker
lineRenderer.SetVertexCount(markerList.Count);
for(var i = 0; i < markerList.Count; i++){
if(markerList.Count >= 2){
lineRenderer.SetPosition(i, markerList[i].transform.position);
}
}
//Plots the markers and stores their locations in the markerList array
if(Input.GetMouseButtonDown(0)){
AddMarker();
}
//Runs the whole thing; makes the cube follow the path
if(Input.GetKeyDown("z")){
Reset();
RunIt();
}
}
function AddMarker(){
//Get the position where the user clicked
var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
var selected : RaycastHit;
Physics.Raycast(ray, selected);
//Instantiate and store the marker in the array
var tempObj : GameObject = Instantiate(marker, selected.point, transform.rotation) as GameObject;
yield;
if(!tempObj){
print("failed to instantiate tempobj");
}
else {
markerList.Add(tempObj);
}
//print(markerList[markerList.Count - 1].transform.position);
}
function MoveObject (thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float) {
var i = 0.0;
var rate = 1.0/time;
while (i < 1.0) {
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield;
}
}
function RunIt(){
var i : int;
for(i = 0; i < markerList.Count; i++){
if(i +1 < markerList.Count){ //prevents the direction from being calculated at the last marker (which doesn't have a next marker to subtract from)
var direction : Vector3 = markerList[i+1].transform.position - markerList[i].transform.position;
cube.transform.rotation = Quaternion.LookRotation(direction);
print("rotation applied at " + Time.time);
}
MoveObject(cube, cube.transform.position, markerList[i].transform.position, 1.0);
print("object moved at " + Time.time);
yield WaitForSeconds(1.5);
}
}
function Reset(){
var firstPos = markerList[0].transform.position;
cube.transform.position = markerList[0].transform.position;
}
I might be totally off or missing the point, but I always look for simple solutions first. so it’s ‘looking’ at the point that’s two points ahead instead of the next point? Maybe change:
var direction : Vector3 = markerList[i+1].transform.position - markerList[i].transform.position;
to
var direction : Vector3 = markerList[i].transform.position - markerList[i-1].transform.position;
You’ll have to make some adjustments so that it doesnt reference markerList[-1] like starting your i loop at 1 instead of zero.