RPG黑暗之光Part3:角色控制
1.在上个章节基础上在Project下的_scenes文件夹ctrl+d一个新的场景,然后删除多余UI控件。拖拽一个游戏角色用来设置这一part的脚本。首先设置角色点击地面的绿色动画,在RPG中的Effect2包下可以找到绿色图标。接着在角色中创建脚本用来控制显示图标。
主要用的方法是在Update()使用以下方法注意这里摄像机的tag要设置为main。
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
bool isCoilder = Physics.Raycast(ray, out hitInfo);
if(isCoilder && hitInfo.collider.tag == "Ground")
{
Instantiate(effect_click, hitInfo.point,Quaternion.identity);
}
2.接着我们利用LookAt方法设置人物指向位置,这里我们用到的是点击以后显示一次绿色图标,然后人物跟着鼠标方向移动直到鼠标抬起。注意这里人物LookAt的位置只需要xz平面,y轴不要变动,不然人物会倒下或者仰起。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour {
public GameObject effect_click;
public bool isClick = false;
public Vector3 targetPoint;
private void Start()
{
targetPoint = transform.position;
}
void Update () {
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
bool isCoilder = Physics.Raycast(ray, out hitInfo);
if (isCoilder && hitInfo.collider.tag == "Ground" && !isClick)
{
targetPoint = new Vector3(hitInfo.point.x, hitInfo.point.y + 0.1f, hitInfo.point.z);
Instantiate(effect_click, targetPoint, Quaternion.identity);
isClick = true;
}
targetPoint = new Vector3(targetPoint.x, transform.position.y, targetPoint.z);
transform.LookAt(targetPoint);
}
if (Input.GetMouseButtonUp(0))
{
isClick = false;
}
targetPoint = new Vector3(targetPoint.x, transform.position.y, targetPoint.z);
transform.LookAt(targetPoint);
}
}
注意这里设置的isclick在Unity中可能会被更改所以后来我改成private了。
扫描二维码关注公众号,回复:
2198184 查看本文章
这里是bug易出现的地方,主要的问题就是左键抬起以后人物会因为地形改变移动方向导致永远到不了目标位置,使得人物一直移动。
3.接着我们来控制人物行走,首先在Unity界面添加一个脚本CharacterController用来使用最简单的移动方式。由于制作后期角色还有其他行动,所以创建一个脚本PlayerBehavior用来显示动画效果。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
enum PlayerStatue
{
Idle,
Run,
}
public class PlayerBehavior : MonoBehaviour {
private CharacterController controller;
private PlayerMove move;
private Animation ant;
public float speed = 1f;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
move = GetComponent<PlayerMove>();
ant = GetComponent<Animation>();
}
// Update is called once per frame
void LateUpdate () {
if (Vector3.Distance(move.targetPoint, transform.position) > 0.1f)
{
controller.SimpleMove(transform.forward * speed);
ant.CrossFade("Sword-Run");
}
else
{
ant.CrossFade("Sword-Idle");
}
}
}
4.人物控制已经结束,我们接下来设置摄像机的跟随移动,在Unity官方demo第一章就有讲解,这里我们在他的基础上还要加上镜头的拉近拉远和旋转操作。
镜头的拉进拉远很好想,将摄像机与主角之间设定的距离进行修改就可以。
void ScrollView()
{
distance = Vector3.Distance(player.position,transform.position);
distance -= Input.GetAxis("Mouse ScrollWheel")*scrollspeed;
distance = Mathf.Clamp(distance, 3f, 11f);
//这里当作是一个法向量就行
offset = Vector3.Normalize(offset) * distance;
transform.position = player.position + offset;
}
相机的旋转需要注意这里旋转轴心的选定
void RotateView()
{
if (Input.GetMouseButton(1))
{
float moveXZ = Input.GetAxis("Mouse X");
//这里xz平面旋转轴心就是y轴
transform.RotateAround(player.position, Vector3.up, moveXZ);
Vector3 originalPos = transform.position;
Quaternion originalRot = transform.rotation;
float moveYZ = Input.GetAxis("Mouse Y");
//自己体会,如果设置不是摄像机的z轴旋转操作问题很大
transform.RotateAround(player.position, transform.right, moveYZ);
float x = transform.eulerAngles.x;
//设置一下yz轴的视角调整不超过头顶
if (x<9 || x > 80)
{
transform.position = originalPos;
transform.rotation = originalRot;
}
offset = transform.position - player.position;
}
}
将这两个方法写在FixedUpdate( )中就可以。