项目02《游戏-11-开发》Unity3D
基于 项目02《游戏-10-开发》Unity3D ,
任务:飞行坐骑
首先创建脚本,
绑定脚本,
using UnityEngine;
public class Dragon : MonoBehaviour{
[SerializeField]
private float speed = 10f;
public Transform player;
private bool isInDragon = false;
private Vector3 dragonPosition;// 坐骑位置
void Update(){
if (isInDragon){//下坐骑
if (Input.GetKeyDown(KeyCode.Q)){
isInDragon = false;
dragonPosition = transform.position;// 保存坐骑的位置
player.position = dragonPosition + Vector3.right * 2f;
Camera.main.GetComponent<CameraCtrl>().target = player;
player.gameObject.SetActive(true);
}
else if (Input.GetKey(KeyCode.LeftArrow))
transform.Translate(Vector3.up * speed * Time.deltaTime);
else if (Input.GetKey(KeyCode.RightArrow))
transform.Translate(Vector3.down * speed * Time.deltaTime);
else if (Input.GetKey(KeyCode.UpArrow))
transform.Translate(Vector3.forward * speed * Time.deltaTime);
else if (Input.GetKey(KeyCode.DownArrow))
transform.Translate(Vector3.back * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
transform.Rotate(Vector3.up * speed * Time.deltaTime * 5f * -1);
else if (Input.GetKey(KeyCode.D))
transform.Rotate(Vector3.up * speed * Time.deltaTime * 5f);
}
else{
if (Input.GetKeyDown(KeyCode.Q)){
if (Vector3.Distance(transform.position, player.position) < 10f){
isInDragon = true;
player.gameObject.SetActive(false);
Camera.main.GetComponent<CameraCtrl>().target = transform;
print(transform.position);
}
}
}
}
}
修改脚本:在Camera.cs脚本中添加public公开Transform类型变量 target就够了,
using UnityEngine;
public class CameraCtrl : MonoBehaviour{
public float dis;
public float height;
public float speed;
public Transform target;
Vector3 targetPos;
void Start(){
target = MainGame.player.transform;
}
void Update(){
transform.LookAt(target.position + Vector3.up * 1.5f);
targetPos = target.forward * (-dis) + target.up * height + target.position;
}
void LateUpdate(){
transform.position = Vector3.Lerp(transform.position, targetPos, speed);
}
void SetTarget(){
//将标签为Player的物体设置为跟踪目标
Transform player = GameObject.FindGameObjectWithTag("Player").transform;
if (player != null && target == null)
target = player;
}
}
记得绑定:
运行 人物在坐骑附近时按Q键即可进入坐骑飞行模式,
方向键上/下/左/右分别代表 :前进飞行/后退飞行/上升/下降,
A键:左方向旋转 D键:右方向旋转
再次按Q键:取消飞行
暂时效果图:
现在为坐骑【龙】添加动画器,如果动画器部分不明白可以看此项目的第一集,
绑定动画器:
在修改坐骑脚本之前:让坐骑【龙】的飞行带有动画更生动形象,
我们要加一行代码让人物骑在龙身上,
首先在龙预制体空间中在其子物体添加人物并设置成隐藏状态命名为Player1
修改坐骑龙代码:
using UnityEngine;
public class Dragon : MonoBehaviour{
[SerializeField]
private float speed = 10f;
public Transform player;
private bool isInDragon = false;
private Vector3 dragonPosition;// 坐骑位置
void Update(){
if (isInDragon){//下坐骑
if (Input.GetKeyDown(KeyCode.Q)){
isInDragon = false;
dragonPosition = transform.position;// 保存坐骑的位置
player.position = dragonPosition + Vector3.right * 2f;
Camera.main.GetComponent<CameraCtrl>().target = player;
transform.Find("Player1").gameObject.SetActive(false);
player.gameObject.SetActive(true);
}
else if (Input.GetKey(KeyCode.LeftArrow))
transform.Translate(Vector3.up * speed * Time.deltaTime);
else if (Input.GetKey(KeyCode.RightArrow))
transform.Translate(Vector3.down * speed * Time.deltaTime);
else if (Input.GetKey(KeyCode.UpArrow))
transform.Translate(Vector3.forward * speed * Time.deltaTime);
else if (Input.GetKey(KeyCode.DownArrow))
transform.Translate(Vector3.back * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
transform.Rotate(Vector3.up * speed * Time.deltaTime * 5f * -1);
else if (Input.GetKey(KeyCode.D))
transform.Rotate(Vector3.up * speed * Time.deltaTime * 5f);
}
else{
if (Input.GetKeyDown(KeyCode.Q)){
if (Vector3.Distance(transform.position, player.position) < 10f){
isInDragon = true;
player.gameObject.SetActive(false);
transform.Find("Player1").gameObject.SetActive(true);
Camera.main.GetComponent<CameraCtrl>().target = transform;
print(transform.position);
}
}
}
}
}
接着实现动画部分代码:
using UnityEngine;
public class Dragon : MonoBehaviour{
[SerializeField]
private float speed = 10f;
public Transform player;
private bool isInDragon = false;
private Vector3 dragonPosition;// 坐骑位置
Animator animator;
void Start(){
animator = GetComponent<Animator>();
}
void Update(){
if (isInDragon){//下坐骑
if (Input.GetKeyDown(KeyCode.Q)){
animator.SetBool("IsFly", false);
isInDragon = false;
dragonPosition = transform.position;// 保存坐骑的位置
player.position = dragonPosition + Vector3.right * 2f;
Camera.main.GetComponent<CameraCtrl>().target = player;
transform.Find("Player1").gameObject.SetActive(false);
player.gameObject.SetActive(true);
}
else if (Input.GetKey(KeyCode.LeftArrow))
transform.Translate(Vector3.up * speed * Time.deltaTime);
else if (Input.GetKey(KeyCode.RightArrow))
transform.Translate(Vector3.down * speed * Time.deltaTime);
else if (Input.GetKey(KeyCode.UpArrow))
transform.Translate(Vector3.forward * speed * Time.deltaTime);
else if (Input.GetKey(KeyCode.DownArrow))
transform.Translate(Vector3.back * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
transform.Rotate(Vector3.up * speed * Time.deltaTime * 5f * -1);
else if (Input.GetKey(KeyCode.D))
transform.Rotate(Vector3.up * speed * Time.deltaTime * 5f);
}
else{//上坐骑
if (Input.GetKeyDown(KeyCode.Q)){
if (Vector3.Distance(transform.position, player.position) < 5f){
isInDragon = true;
//player.gameObject.SetActive(false);
GameObject.Find("Player").SetActive(false);
transform.Find("Player1").gameObject.SetActive(true);
Camera.main.GetComponent<CameraCtrl>().target = transform;
animator.SetBool("IsFly", true);
}
}
}
}
}
运行即可实现飞行:
运行 人物在坐骑附近时按Q键即可进入坐骑飞行模式,
方向键上/下/左/右分别代表 :前进飞行/后退飞行/上升/下降,
A键:左方向旋转 D键:右方向旋转
再次按Q键:取消飞行
在龙附近处按Q键:
左右键控制上升下降,
AD键控制左转右转,
上下键控制前进后退,
右方向键下落 再次按Q键实现降落,
End.