- Home /
Question by
xiaohe · Aug 19, 2011 at 02:42 AM ·
camerajavascriptmovepath
Viewport in the game, how to create a path~
In the game window, you can create your own at some point generate a path. You can add a camera to follow the path。 This camera can generate different paths 。 Click the Generate button to see the different camera paths! thank you!!
var cube1 :Transform; var speed : float = 2;
//private var currentWaypoint : int = 0;
//var waypoint : Vector3[];
function OnGUI () { GUI.Button(Rect(0,0,100,30),"add path"); GUI.Button(Rect(0,35,100,30),"add Camera");
}
function Update () {
var hit :RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray,hit,1000)) {
if(Input.GetMouseButtonDown(0)){
var cube2 =Instantiate(cube1,transform.position,Quaternion.identity);
cube2.transform.position=hit.point;
}
}
//hit.point- Camera.main.transform.position
var relativePos : Vector3 = cube2.transform.position ;
//var rotation : Quaternion = Quaternion.LookRotation(relativePos);
transform.position = Vector3.MoveTowards(transform.position, relativePos, Time.deltaTime * speed);
//transform.LookAt(target);
}
Comment
Answer by Techsuport · Aug 19, 2011 at 03:09 AM
it seems you want to make paths by clicking and having those points listed to move to points down the list... something like this?
var pathList:Array;
var currentTarg:Vector3;
var proximityMin:float;
var walker:GameObject;
var speed:float;
function Update(){
if(Input.GetButtonDown("Fire1")){
var hit:RaycastHit;
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition,hit))){
pathList.Add(hit.point);
}
}
}
function FixedUpdate(){
if(walker&&pathList.length>0){
walker.transform.position=walker.transform.position+((walker.transform.position-currentTarg).normalized*speed*Time.deltaTime);
if(Vector3.Distance(walker.transform.position,pathList[0])<proximityMin){
pathList.Shift();
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612184004im_/https://answers.unity.com/themes/thub/images/avi.jpg)