1.创建物体
在Hierarchy面板空白地方右键->3D Object创建一个Plane和一个Capsule
2.添加组件
鼠标单击选中Capsule
在右侧Inspector面板删除Capsule Collider碰撞体组件
添加一个Character Controller(角色控制器,自带一个碰撞体)和一个脚本ThirdPersonController(名字可以自己取)
3.编写代码
在ThirdPersonController
中定义变量:
public Transform cameraTransform,cameraPole;
public float moveSpeed,cameraSpeed;
public LayerMask cameraLayers;
CharacterController character;
float cameraPitch,maxDistance;
float leftId, rightId, halfWidth;
Vector2 startPos,lookInput,moveInput;
在Start
中初始化变量
void Start()
{
leftId = -1;
rightId = -1;
halfWidth = Screen.width / 2;
character = GetComponent<CharacterController>();
cameraPitch = cameraPole.localRotation.eulerAngles.x;
maxDistance = cameraTransform.localPosition.z;
}
创建一个函数 GetTouchInput
// 获取手指触摸的函数
void GetTouchInput()
{
//获取每一个手指的触摸信息
foreach(Touch t in Input.touches)
{
switch (t.phase)
{
case TouchPhase.Began:
//点击接触
if (t.position.x < halfWidth && leftId == -1)
{
leftId = t.fingerId;
startPos = t.position;
}else if (t.position.x > halfWidth && rightId == -1)
{
rightId = t.fingerId;
}
break;
case TouchPhase.Ended:
//手指离开
case TouchPhase.Canceled:
//系统取消跟踪手指
if(t.fingerId == leftId)
{
leftId = -1;
//isMoveing = false;
}else if (t.fingerId == rightId)
{
rightId = -1;
}
break;
case TouchPhase.Moved:
//手指移动
if (t.fingerId == leftId)
{
moveInput = t.position - startPos;
}else if (t.fingerId == rightId)
{
lookInput = t.deltaPosition * cameraSpeed * Time.deltaTime;
}
break;
case TouchPhase.Stationary:
//手指静止
if(t.fingerId == rightId)
{
lookInput = Vector2.zero;
}
break;
}
}
}
创建一个LookAround
void LookAround()
{
cameraPitch = Mathf.Clamp(cameraPitch - lookInput.y, -90f, 90f);
cameraTransform.localRotation = Quaternion.Euler(cameraPitch, 0, 0);
transform.Rotate(transform.up, lookInput.x);
}
创建一个Move
void Move()
{
Vector2 moveDir = moveInput.normalized * moveSpeed*Time.deltaTime;
character.Move(transform.right * moveDir.x + transform.forward * moveDir.y);
}
创建一个MoveCamera
void MoveCamera()
{
Vector3 rayDir = cameraTransform.position - cameraPole.position;
if(Physics.Raycast(cameraPole.position,rayDir,out RaycastHit hit, Mathf.Abs(maxDistance), cameraLayers))
{
cameraTransform.position = hit.point;
}
else
{
cameraTransform.localPosition = new Vector3(0, 1.2f, maxDistance);
}
}
在Update中调用 GetTouchInput
、LookAround
、Move
、MoveCamera
。
void Update()
{
GetTouchInput();
if (leftId != -1)
{
Move();
}
if (rightId != -1)
{
LookAround();
}
}
在FixedUpdate中调用 MoveCamera
。
private void FixedUpdate()
{
MoveCamera();
}
最后ThirdPersonController
脚本代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonController : MonoBehaviour
{
public Transform cameraTransform,cameraPole;
public float moveSpeed,cameraSpeed;
public LayerMask cameraLayers;
CharacterController character;
float cameraPitch,maxDistance;
float leftId, rightId, halfWidth;
Vector2 startPos,lookInput,moveInput;
// Start is called before the first frame update
void Start()
{
leftId = -1;
rightId = -1;
halfWidth = Screen.width / 2;
character = GetComponent<CharacterController>();
cameraPitch = cameraPole.localRotation.eulerAngles.x;
maxDistance = cameraTransform.localPosition.z;
}
// Update is called once per frame
void Update()
{
GetTouchInput();
if (leftId != -1)
{
Move();
}
if (rightId != -1)
{
LookAround();
}
}
private void FixedUpdate()
{
MoveCamera();
}
// 获取手指触摸的函数
void GetTouchInput()
{
//获取每一个手指的触摸信息
foreach(Touch t in Input.touches)
{
switch (t.phase)
{
case TouchPhase.Began:
//点击接触
if (t.position.x < halfWidth && leftId == -1)
{
leftId = t.fingerId;
startPos = t.position;
}else if (t.position.x > halfWidth && rightId == -1)
{
rightId = t.fingerId;
}
break;
case TouchPhase.Ended:
//手指离开
case TouchPhase.Canceled:
//系统取消跟踪手指
if(t.fingerId == leftId)
{
leftId = -1;
//isMoveing = false;
}else if (t.fingerId == rightId)
{
rightId = -1;
}
break;
case TouchPhase.Moved:
//手指移动
if (t.fingerId == leftId)
{
moveInput = t.position - startPos;
}else if (t.fingerId == rightId)
{
lookInput = t.deltaPosition * cameraSpeed * Time.deltaTime;
}
break;
case TouchPhase.Stationary:
//手指静止
if(t.fingerId == rightId)
{
lookInput = Vector2.zero;
}
break;
}
}
}
void LookAround()
{
cameraPitch = Mathf.Clamp(cameraPitch - lookInput.y, -90f, 90f);
cameraTransform.localRotation = Quaternion.Euler(cameraPitch, 0, 0);
transform.Rotate(transform.up, lookInput.x);
}
void Move()
{
Vector2 moveDir = moveInput.normalized * moveSpeed*Time.deltaTime;
character.Move(transform.right * moveDir.x + transform.forward * moveDir.y);
}
void MoveCamera()
{
Vector3 rayDir = cameraTransform.position - cameraPole.position;
if(Physics.Raycast(cameraPole.position,rayDir,out RaycastHit hit, Mathf.Abs(maxDistance), cameraLayers))
{
cameraTransform.position = hit.point;
}
else
{
cameraTransform.localPosition = new Vector3(0, 1.2f, maxDistance);
}
}
}
4.设置
在Capsule中创建一个空对象并改名为Camera Pole,Rotation的x设置为10
文章来源:https://www.toymoban.com/news/detail-781148.html
把相机放Camera Pole里,Position设置为(0,1.2,-5)
Capsule的ThirdPersonController
设置为
现在可以编译成apk文件安装运行了!文章来源地址https://www.toymoban.com/news/detail-781148.html
到了这里,关于Unity移动端第三人称控制器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!