按惯例,先上效果图
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICdzFWRoRXdvN1LclHdpZXYyd2LcBzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX90zdaVXOXFGN1cVYxljRiZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39DN0cjM1EDM5ETNygDM0EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
1、新建一个地形,导入terrrain asset,把地面设成草地。
2、新建一个光源,不然地图会很暗
3、导入角色模型,在inspector调整scale,使人物的大小适合屏幕
4、使相机跟随人物,即在人物模型的后上方,这部分要用脚本实现,新建一个MyCamera的c#脚本,然后把它拖给主相机,并把人物模型的tag改为player,
代码如下
using UnityEngine;
using System.Collections;
public class MyCamera : MonoBehaviour {
public float m_distanceAway = 4.5f;
public float m_distanceUp = 1.76f;
public float m_smooth = 5;
public Transform m_player;
private Transform m_transsform;
// Use this for initialization
void Start () {
m_transsform = this.transform;
m_player = GameObject.FindGameObjectWithTag("Player").transform;
}
// Update is called once per frame
void Update () {
float m_wangtedRotationAngle = m_player.eulerAngles.y;
float m_wangtedHeight = m_player.transform.position.y + m_distanceUp;
float m_currentRotationAngle = m_transsform.eulerAngles.y;
float m_currentHeight = m_transsform.position.y;
m_currentRotationAngle = Mathf.LerpAngle(m_currentRotationAngle, m_wangtedRotationAngle, m_smooth * Time.deltaTime);
m_currentHeight = Mathf.Lerp(m_currentHeight, m_wangtedHeight, m_smooth * Time.deltaTime);
Quaternion m_currentRotation = Quaternion.Euler(0, m_currentRotationAngle, 0);
Vector3 m_position = m_player.transform.position;
m_position -= m_currentRotation * Vector3.forward * m_distanceAway;
m_position = new Vector3(m_position.x, m_currentHeight, m_position.z);
m_transsform.position = Vector3.Lerp(m_transsform.position, m_position, Time.time);
m_transsform.LookAt(m_player);
RaycastHit hit;
if (Physics.Linecast(m_player.position + Vector3.up, m_transsform.position, out hit)) //这里开始主要是使相机不会穿透模型
{
string name = hit.collider.gameObject.tag;
if (name != "MainCamera")
{
float currentDistance = Vector3.Distance(hit.point, m_player.position);
if (currentDistance < m_distanceAway)
{
m_transsform.position = hit.point;
}
}
}
}
}
到这里运代项目就可以看到相机跟着人物模型了,下面加入easy touch
5、前面已经导入了easy touch,新建一个虚拟摇杆
在Game窗口就可以看到虚拟摇杆了,不过现在还不能操作角色,同时在Hierarchy窗口多了一个EayTouch和New joystick
6、EasyTouch不用管它,改一下New joystick的属性,如图
这个一定要改,其他属性自己看着改吧
7、新建一个PlayerCtr的c#脚本,并拖给人物模型,代码如下
using UnityEngine;
using System.Collections;
public class PlayerCtr : MonoBehaviour {
public Transform m_transform;
// Use this for initialization
void Start () {
m_transform = this.transform;
}
// Update is called once per frame
void Update () {
}
void OnEnable()
{
Debug.Log("OnEnable()");
EasyJoystick.On_JoystickMove += OnJoystickMove;
EasyJoystick.On_JoystickMoveEnd += OnJoystickMoveEnd;
// EasyButton.On_ButtonUp += On_ButtonUp;
}
void OnDisable()
{
EasyJoystick.On_JoystickMove -= OnJoystickMove;
EasyJoystick.On_JoystickMoveEnd -= OnJoystickMoveEnd;
// EasyButton.On_ButtonUp -= On_ButtonUp;
}
void OnDestroy()
{
EasyJoystick.On_JoystickMove -= OnJoystickMove;
EasyJoystick.On_JoystickMoveEnd -= OnJoystickMoveEnd;
// EasyButton.On_ButtonUp -= On_ButtonUp;
}
void OnJoystickMoveEnd(MovingJoystick move)
{
Debug.Log("OnJoystickMoveEnd()");
animation.CrossFade("Idle");
}
void OnJoystickMove(MovingJoystick move)
{
Debug.Log("-----------------------------");
Debug.Log("OnJoystickMove()");
float joyPositionX = move.joystickAxis.x;
float joyPositionY = move.joystickAxis.y;
if (joyPositionY != 0 || joyPositionX != 0)
{
//设置角色的朝向(朝向当前坐标+摇杆偏移量)
m_transform.LookAt(new Vector3(m_transform.position.x - joyPositionX, m_transform.position.y, m_transform.position.z - joyPositionY));
//移动玩家的位置(按朝向位置移动)
m_transform.Translate(Vector3.forward * Time.deltaTime * 7.5F);
//播放奔跑动画
animation.CrossFade("Run");
}
}
}
8、已经完成了,可以跑起来试一下了
工程文件