给Zombunny加一个tag,脚本用
p3玩家可以围着Zombunny转,仅仅是演示
p4鼠标控制player转向,wasd控制移动
添加quad改名为floor,鼠标每次点击会落到floor上(脚本实现)
添加layer的floor
并移除floor的component
写floor的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovemment : MonoBehaviour
{
public float speed = 6f;
Rigidbody playerRigidbody;
int floorMask;
//Transform enemyTransform;
Animator animator;
private void Awake() // 加载脚本实例调用Awake
{
playerRigidbody = GetComponent(); // MonoBehaviour中定义的方法GetComponent找到有Rigidbody的游戏对象
floorMask = LayerMask.GetMask("Floor");
//var enemy = GameObject.FindGameObjectWithTag("Zombunny");
//enemyTransform = enemy.transform; // 通过tag获取enemy的Transform
animator = GetComponent();
}
private void FixedUpdate() // 固定方法名,时刻调用MonoBehaviour更新游戏画面
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical"); // 获取方向轴的输入
Move(h, v);
Turning();
DoAnimation(h, v);
}
private void DoAnimation(float h, float v)
{
bool isWalking = h != 0 || v != 0;
animator.SetBool("IsWalking", isWalking);
}
private void Move(float h, float v)
{
Vector3 movement = new Vector3(); // 两个方向键同时按斜着走要算斜率
movement.Set(h, 0f, v);
movement = movement.normalized * Time.deltaTime * speed; // 比喻:1秒显示60FPS画面
playerRigidbody.MovePosition(transform.position + movement); // player的坐标加上移动
}
private void Turning()
{
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(camRay, out RaycastHit floorHit, 100, floorMask))
{
Vector3 forward = floorHit.point - transform.position;
forward.y = 0f;
Quaternion lookQuaternion = Quaternion.LookRotation(forward);
playerRigidbody.MoveRotation(lookQuaternion);
}
//Vector3 forward = enemyTransform.position - enemyTransform.position; // player和enemy的距离
//Quaternion lookQuaternion = Quaternion.LookRotation(forward); // unity计算player对enemy的forward
//playerRigidbody.MoveRotation(lookQuaternion);
}
}
p5添加player移动动画
添加AnimationController拖拽到player上
在AC里拖拽完成动画的转换
写DoAnimation方法捕捉动画
效果
p6摄像机跟随角色
创建camera脚本拖拽给main camera
暂时可以穿模
p7怪物跟随玩家
添加障碍,给player add component - physics - capsule collider,覆盖player
给Zombunny也添加一个capsule collider
不穿模了
打开window-ai-navigation,敌人可以自动寻路
给zombunny add component-nav mesh agent
不会打圈的设定
换成上帝视角
好了
作者:儒雅随和