本文主要是介绍Unity3D 学习笔记(八) 锁定目标与 Physics.OverlapBox,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
添加所需变量
添加变量
public bool lockon;
新button
public Button lockButton = new Button();
以及
lockButton.Tick(Input.GetKey(keyLock));
lockon = lockButton.OnPressed;
调取这个信号
if(pi.lockon){camcon.LockSwitch();}
在CameraController中
public void LockSwitch(){print("lock");}
尝试lock可以正常调用
利用OverlapBox检测碰撞
检测target使用API Physics.OverlapBox
public static Collider[] **OverlapBox**([Vector3](https://docs.unity3d.com/ScriptReference/Vector3.html) **center**, [Vector3](https://docs.unity3d.com/ScriptReference/Vector3.html) **halfExtents**, [Quaternion](https://docs.unity3d.com/ScriptReference/Quaternion.html) **orientation** = Quaternion.identity, int **layerMask** = AllLayers, [QueryTriggerInteraction](https://docs.unity3d.com/ScriptReference/QueryTriggerInteraction.html) **queryTriggerInteraction** = QueryTriggerInteraction.UseGlobal);
参数 Vector3类型的BOX中心,三个维度下BOX的半长组成的Vector3,Quaternion类型的BOX的方向,碰撞的Layer,默认为所以的Layer,最后的queryTrigger暂时不需要。
返回值为与BOX碰撞的Collider
在CameraController中声明新变量
private GameObject lockTarget;
完善LockSwitch函数,使其能计算出锁定目标,将其赋值给lockTarget
public void LockSwitch(){print("lock");//if(lockTarget == null){//try to lock othersVector3 modelOrigin1 = model.transform.position;Vector3 modelOrigin2 = modelOrigin1 + new Vector3(0,1,0);//计算出人脑袋的位置 Vector3 boxCenter = modelOrigin2 + model.transform.forward *5.0f;//???Collider[] cols = Physics.OverlapBox(boxCenter,new Vector3(0.5f,0.5f,5f),model.transform.rotation,LayerMask.GetMask("Enemy"));if(cols.Length == 0){lockTarget = null;}else {foreach(var col in cols){//print(col.name);if(lockTarget == col.gameObject){lockTarget = null;print("lock cancel");break;}else{lockTarget = col.gameObject;print(lockTarget);break;}}}}
修改相机控制中的追踪部分,当无目标时与原来相同,有锁定目标时视线始终看向目标并调整角色朝向
void FixedUpdate(){if(lockTarget == null){Vector3 tempModeEuler = model.transform.eulerAngles;playerHandle.transform.Rotate(Vector3.up,pi.Jright * horizontalSpeed *Time.fixedDeltaTime);//cameraHandle.transform.Rotate(Vector3.right,pi.Jup * -verticalSpeed *Time.deltaTime);//tempEulerX = cameraHandle.transform.eulerAngles.x;tempEulerX -= pi.Jup * verticalSpeed *Time.deltaTime;tempEulerX = Mathf.Clamp(tempEulerX,-40,30);cameraHandle.transform.localEulerAngles = new Vector3(tempEulerX,0,0);model.transform.eulerAngles = tempModeEuler;}else {Vector3 tempForward = lockTarget.transform.position - model.transform.position;tempForward.y = 0;playerHandle.transform.forward = tempForward;}cam.transform.position = Vector3.SmoothDamp(cam.transform.position,transform.position,ref cameraDampVelocity,0.5f);//cam.transform.eulerAngles = transform.eulerAngles;cam.transform.LookAt(cameraHandle.transform);//一直看着camerahandle}
可以实现视角的锁定
显示锁定目标的圆圈
创建一个UI里面的Image
选择合适的图形、大小、颜色(在game窗口看到这个image)
在cameraController中添加
using UnityEngine.UI;
public Image lockDot;
在start中使其初始不可用
lockDot.enabled = false;
在分支中添加对应的启用和禁用
此时进游戏发现点在视野中央,但是由于摄像机的跟随效果所以其与锁定目标有所便宜,因此在FixedUpdate中的有锁定目标分支添加
lockDot.transform.position = Camera.main.WorldToScreenPoint (lockTarget.transform.position);
再打开游戏,可以实现效果
这篇关于Unity3D 学习笔记(八) 锁定目标与 Physics.OverlapBox的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!