您好,登錄后才能下訂單哦!
//人物方向和判斷點擊事件加點擊特效
using UnityEngine;
using System.Collections;
public class playerDir : MonoBehaviour {
public GameObject effect_click_prefab;
private bool isMoving=false;//鼠標是否被按下
public Vector3 targetPosition=Vector3.zero;//目標位置
private PlayerMove playerMove;
void Start()
{
targetPosition=transform.position;
playerMove=this.GetComponent<PlayerMove>();
}
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//拿到鼠標按下的點
RaycastHit hitInfo;
bool isCollider=Physics.Raycast(ray,out hitInfo);//定義一個射線
if(isCollider&&hitInfo.collider.tag==Tags.ground)//判斷射線是否和地面接觸碰撞
{
//判斷是否點擊到了UI層的東西
if(UICamera.hoveredObject.tag!="Accept"&&UICamera.hoveredObject.tag!="Inventory_item_grid"&&UICamera.hoveredObject.tag!="Inventory_item")
{
isMoving=true;
ShowClickEffect(hitInfo.point);
LookAtTarget(hitInfo.point);
}
}
}
//按下鼠標左建的時候停止運動
if(Input.GetMouseButtonUp(0))
{
isMoving=false;
}
if(isMoving)
{
//得到需要移動到的目標位置
//讓角色朝向目標位置
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//拿到鼠標按下的點
RaycastHit hitInfo;
bool isCollider=Physics.Raycast(ray,out hitInfo);//定義一個射線
if(isCollider&&hitInfo.collider.tag==Tags.ground)//判斷射線是否和地面接觸碰撞
{
//讓當前對象對著射線的方向
LookAtTarget(hitInfo.point);
}
}
else
{
if(playerMove.isMoving)
{
LookAtTarget(targetPosition);
}
}
}
//實例化顯示特效效果
void ShowClickEffect(Vector3 hitPoint)
{
hitPoint=new Vector3(hitPoint.x,hitPoint.y+0.1f,hitPoint.z);
GameObject.Instantiate(effect_click_prefab,hitPoint,Quaternion.identity);
}
//讓角色朝向目標位置和朝向的改變
void LookAtTarget(Vector3 hitPoint)
{
targetPosition=hitPoint;
targetPosition=new Vector3(targetPosition.x,transform.position.y,targetPosition.z);//得到主角朝向位置
this.transform.LookAt(targetPosition);//讓主角改變
}
}
//人物移動
using UnityEngine;
using System.Collections;
//角色狀態
public enum PlayerState
{
Moving,//移動
Idle //閑著
}
public class PlayerMove : MonoBehaviour {
public float speed=4f;
public PlayerState state=PlayerState.Idle;//默認狀態為Idle
private playerDir dir;
private CharacterController controller;//角色控制器
public bool isMoving=false;
void Start()
{
dir=this.GetComponent<playerDir>();
controller=this.GetComponent<CharacterController>();
}
void Update ()
{
float distance=Vector3.Distance(dir.targetPosition,transform.position);//計算目標位置到當前位置
if(distance>0.3f)
{
isMoving=true;
state=PlayerState.Moving;
controller.SimpleMove(transform.forward*speed);//角色移動
}
else
{
isMoving=false;
state=PlayerState.Idle;
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。