using UnityEngine;
using System.Collections;
public class PlayerMoveToTarget : MonoBehaviour {
public CharacterController[] players;
public Vector3[] targetPosition;
private int rotationSpeed = 5;
private float speed = 0.05f;
public Camera camera;
void Start(){
targetPosition = new Vector3[players.Length];
for(int i=0;i<players.Length;i++){
targetPosition[i]=Vector3.zero;
}
}
void Update () {
if(Input.GetMouseButtonUp(0)){
SetTargetPosition();
}
for(int i=0;i<players.Length;i++){
if (targetPosition[i]!=Vector3.zero){
this.MoveTo(i);
}
}
}
void SetTargetPosition(){
Vector3 cursorScreenPosition = Input.mousePosition;//鼠标在屏幕上的位置
Ray ray = camera.ScreenPointToRay(cursorScreenPosition);//在鼠标所在的屏幕位置发出一条射线(暂名该射线为x射线)
RaycastHit hit;
if(Physics.Raycast(ray,out hit)){
if(hit.collider.gameObject.name=="Terrain"){//设置地形name为Terrain
for(int i=0;i<players.Length;i++){
targetPosition[i]=hit.point;
}
}
}
}
public void MoveTo(int controllerIndex){
if(Mathf.Abs(Vector3.Distance(targetPosition[controllerIndex], players[controllerIndex].transform.position))<1.3f){
targetPosition[controllerIndex] = Vector3.zero;
return;
}
players[controllerIndex].transform.LookAt(new Vector3(targetPosition[controllerIndex].x,players[controllerIndex].transform.position.y,targetPosition[controllerIndex].z));
Vector3 v = Vector3.ClampMagnitude(targetPosition[controllerIndex] - players[controllerIndex].transform.position,speed);
players[controllerIndex].Move(v);
}
}
using System.Collections;
public class PlayerMoveToTarget : MonoBehaviour {
public CharacterController[] players;
public Vector3[] targetPosition;
private int rotationSpeed = 5;
private float speed = 0.05f;
public Camera camera;
void Start(){
targetPosition = new Vector3[players.Length];
for(int i=0;i<players.Length;i++){
targetPosition[i]=Vector3.zero;
}
}
void Update () {
if(Input.GetMouseButtonUp(0)){
SetTargetPosition();
}
for(int i=0;i<players.Length;i++){
if (targetPosition[i]!=Vector3.zero){
this.MoveTo(i);
}
}
}
void SetTargetPosition(){
Vector3 cursorScreenPosition = Input.mousePosition;//鼠标在屏幕上的位置
Ray ray = camera.ScreenPointToRay(cursorScreenPosition);//在鼠标所在的屏幕位置发出一条射线(暂名该射线为x射线)
RaycastHit hit;
if(Physics.Raycast(ray,out hit)){
if(hit.collider.gameObject.name=="Terrain"){//设置地形name为Terrain
for(int i=0;i<players.Length;i++){
targetPosition[i]=hit.point;
}
}
}
}
public void MoveTo(int controllerIndex){
if(Mathf.Abs(Vector3.Distance(targetPosition[controllerIndex], players[controllerIndex].transform.position))<1.3f){
targetPosition[controllerIndex] = Vector3.zero;
return;
}
players[controllerIndex].transform.LookAt(new Vector3(targetPosition[controllerIndex].x,players[controllerIndex].transform.position.y,targetPosition[controllerIndex].z));
Vector3 v = Vector3.ClampMagnitude(targetPosition[controllerIndex] - players[controllerIndex].transform.position,speed);
players[controllerIndex].Move(v);
}
}