方法一
这个方法适用于第三人称角色控制,摄像机以固定的一个方向跟随角色移动,角色在屏幕上以向上下左右移动的状态显示。
在玩家模型上添加C# Player脚本,并且在摄像机挂载摄像机跟随脚本 FollowTags
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
//速度
public float speed = 1;
//玩家
public Transform player;
//刚体
public Rigidbody PlayerRigidbody;
//平滑值
private float smoothing;
//旋转速度
public float rotateSpeed = 0;
//跳跃的力量
public float jumpForce = 0;
//动画控制器
public Animator Anim;
//移动纵横值
private float h;
private float v;
private void Awake()
{
//获取刚体
PlayerRigidbody = transform.GetComponent<Rigidbody>();
//获取动画控制器
Anim = transform.GetComponent<Animator>();
}
private void Update()
{
//移动
Move();
//旋转
Rotation();
//跳跃
Jump();
}
//移动
public void Move()
{
//获取输入的横向和纵向值
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
//Translate控制移动
//transform.Translate(new Vector3(h, 0, v) * speed * Time.deltaTime);
//刚体移动方法
PlayerRigidbody.MovePosition(transform.position + new Vector3(h, 0, v) * speed * Time.deltaTime);
//动画控制
if (h != 0 || v != 0)
{
Anim.SetBool("Move", true);
}
else
{
Anim.SetBool("Move", false);
}
}
//旋转
public void Rotation()
{
Vector3 targetDir = new Vector3(h, 0, v);
//四元数
Quaternion newRotation = Quaternion.LookRotation(targetDir, Vector3.up);
//旋转值赋值
transform.rotation= Quaternion.Lerp(transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
}
//跳跃
public void Jump()
{
if (Input.GetButtonUp("Jump"))
{
print("jump");
PlayerRigidbody.velocity = new Vector2(PlayerRigidbody.velocity.x, 0);//施加y方向速度,x方向维持原速
PlayerRigidbody.AddForce(new Vector2(PlayerRigidbody.velocity.x, jumpForce));
}
}}
方法二
这个方法适用于第三人称角色控制,摄像机永远在背后跟随角色移动,角色永远向前移动。
在玩家模型上添加C# PlayerRotation 脚本,并把摄像机放到角色里。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerRotation : MonoBehaviour
{
public float moveSpeed = 60f;
public float angularSpeed = 3f;
private Rigidbody PlayerRigidbody;
private float h = 0.0f;
private float v = 0.0f;
public float jumpForce=2000;
private Animator Anim;
// Start is called before the first frame update
void Start()
{
PlayerRigidbody = this.GetComponent<Rigidbody>();
Anim = transform.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//跳
Jump();
//前后移动
Move();
//旋转
Rotation();
}
//移动
public void Move()
{
//获取上下键并控制前后移动
v = Input.GetAxis("Vertical");
//刚体移动方法
PlayerRigidbody.AddForce(transform.forward * v * moveSpeed * Time.deltaTime, ForceMode.Impulse);
//以下移动方法行走时跳不起来
//PlayerRigidbody.velocity = transform.forward * v * moveSpeed * Time.deltaTime;
if (v != 0)
{
Anim.SetBool("Move", true);
}
else
{
Anim.SetBool("Move", false);
}
}
//旋转
public void Rotation()
{
//获取水平按键值并控制左右旋转
h = Input.GetAxis("Horizontal");
//起跳
PlayerRigidbody.angularVelocity = transform.up * h * angularSpeed;
}
//跳跃
public void Jump()
{
if (Input.GetButtonUp("Jump"))
{
print("jump");
PlayerRigidbody.velocity = new Vector2(PlayerRigidbody.velocity.x, 0);//施加y方向速度,x方向维持原速
PlayerRigidbody.AddForce(new Vector2(PlayerRigidbody.velocity.x, jumpForce));
}
}
}
方法三
这个方法适用于第一人称角色控制,在角色身上挂载移动脚本 PlayerRotation,摄像机需添加到骨骼节点Spine2里,节点上需挂载 第三人称视觉脚本 LookAround,并调节旋转角度参数,这样移动鼠标就可以带动画面环顾四周,上身骨骼会跟随鼠标进行上下左右,并且模型下身也会跟随水平旋转。