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