👨💻个人主页:@元宇宙-秩沅
👨💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
👨💻 本文由 秩沅 原创
👨💻 收录于专栏:Unity基础实战
⭐🅰️⭐
文章目录
- ⭐🅰️⭐
- ⭐前言⭐
- 🎶(==A==) 1.资源的导入
- 🎶(==B==) 2.动画的切入以及移动旋转相关
- 🎶(==C==) 3.混合树的添加
- 🎶(==D==) 4.第三人称的移动
- ⭐🅰️⭐
⭐前言⭐
🎶(A) 1.资源的导入
资源地址
🎶(B) 2.动画的切入以及移动旋转相关
1.第一人称的移动——使用角色控制器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 玩家的移动
//___________创建者:___秩沅____
//_____________________________________
//-------------------------------------
public class PlayerMove : MonoBehaviour
{
private float vertical;
private float horizontal;
private float mousePosition;
private CharacterController player; //角色控制器
private Vector3 moveDerictor; //移动的方向
public float velocity = 1f; //移动的速度
public float roVelocity = 10f;
private Animator playerAnimatior;
private void Awake()
{
player = GetComponent<CharacterController>();
playerAnimatior = GetComponent<Animator>();
}
private void FixedUpdate()
{
vertical = Input.GetAxis("Vertical") ;
horizontal = Input.GetAxis("Horizontal") ;
mousePosition = Input.GetAxis("Mouse X");
//旋转
transform.localRotation *= Quaternion.Euler(0,mousePosition*roVelocity,0);
//移动
if(Input.GetAxis("Vertical")!= 0 || Input.GetAxis("Horizontal")!= 0)
{
playerAnimatior.SetBool("move",true);
moveDerictor = new Vector3(horizontal, 0, vertical);
moveDerictor = moveDerictor.normalized; //将方向变成单位向量
player.SimpleMove( transform.forward + moveDerictor*velocity *Time.deltaTime );
//rigidbody.MovePosition( transform.localPosition + moveDerictor * velocity * Time.deltaTime); //速度*方向 = 向量
//此时物体并非跟着自己的旋转方向进行移动而是根据自身位置进行改变
//(白话:无法变成FPS的第一视角进行当前视角当前前进)
}
else
{
print(Input.GetAxis("Vertical"));
print(Input.GetAxis("Horizontal"));
playerAnimatior.SetBool("move", false);
playerAnimatior.SetBool("static", true);
}
}
private void MouseRotation()
{
}
}
2.上帝视角的移动——使用刚体
-
关于相机,此时理想的位置,应该是相机固定跟随在机器人的后面,所以此时需要参照物
-
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 玩家的运动相关
//___________创建者:___秩沅____
//_____________________________________
//-------------------------------------
public class PlayerMove : MonoBehaviour
{
private float vertical;
private float horizontal;
private float mousePosition;
private Rigidbody rigidbody; //玩家的刚体组件
private Vector3 moveDerictor; //移动的方向
public float velocity = 1f; //移动的速度
public float roVelocity = 10f;
private Animator playerAnimatior;
private void Awake()
{
rigidbody = GetComponent<Rigidbody>();
playerAnimatior = GetComponent<Animator>();
}
private void FixedUpdate()
{
vertical = Input.GetAxis("Vertical") ;
horizontal = Input.GetAxis("Horizontal") ;
mousePosition = Input.GetAxis("Mouse X");
//旋转
transform.rotation *= Quaternion.Euler(0,mousePosition*roVelocity,0);
//移动
if(Input.GetAxis("Vertical")!= 0 || Input.GetAxis("Horizontal")!= 0)
{
playerAnimatior.SetBool("move",true);
moveDerictor = new Vector3(horizontal, 0, vertical);
moveDerictor = moveDerictor.normalized; //将方向变成单位向量
rigidbody.MovePosition( transform .position + moveDerictor * velocity * Time.deltaTime); //速度*方向 = 向量
}
else
{
print(Input.GetAxis("Vertical"));
print(Input.GetAxis("Horizontal"));
playerAnimatior.SetBool("move", false);
playerAnimatior.SetBool("static", true);
}
}
private void MouseRotation()
{
}
}
🎶(C) 3.混合树的添加
🎶(D) 4.第三人称的移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 玩家的移动
//___________创建者:___秩沅____
//_____________________________________
//-------------------------------------
public class PlayerMove : MonoBehaviour
{
private float vertical;
private float horizontal;
private float mousePosition;
private CharacterController player; //角色控制器
private Vector3 moveDerictor; //移动的方向
public float velocity = 2f; //移动的速度
public float roVelocity = 10f;
private Animator playerAnimatior;
private void Awake()
{
player = GetComponent<CharacterController>();
playerAnimatior = GetComponent<Animator>();
}
private void FixedUpdate()
{
vertical = Input.GetAxis("Vertical") ;
horizontal = - Input.GetAxis("Horizontal") ;
mousePosition = Input.GetAxis("Mouse X");
//旋转
transform.localRotation *= Quaternion.Euler(0, mousePosition * roVelocity, 0);
if (vertical != 0 ||horizontal != 0)
{
//移动
playerAnimatior.SetFloat("SpeedWS", (int)vertical);
playerAnimatior.SetFloat("SpeedAD", (int)horizontal);
moveDerictor = new Vector3(vertical, 0, horizontal);
print(moveDerictor.normalized);
/// moveDerictor = moveDerictor.normalized; //将方向变成单位向量
//transform.position= transform.position + moveDerictor.normalized*Time .deltaTime ;
player.SimpleMove(transform.forward * vertical );
player.SimpleMove(transform.right * -horizontal);
//GetComponent<Rigidbody>().MovePosition( transform.localPosition + moveDerictor * velocity * Time.deltaTime); //速度*方向 = 向量
//此时物体并非跟着自己的旋转方向进行移动而是根据自身位置进行改变
//(白话:无法变成FPS的第一视角进行当前视角当前前进)
}
}
private void MouseRotation()
{
}
}
⭐🅰️⭐
⭐【Unityc#专题篇】之c#进阶篇】
⭐【Unityc#专题篇】之c#核心篇】
⭐【Unityc#专题篇】之c#基础篇】
⭐【Unity-c#专题篇】之c#入门篇】
⭐【Unityc#专题篇】—进阶章题单实践练习
⭐【Unityc#专题篇】—基础章题单实践练习
⭐【Unityc#专题篇】—核心章题单实践练习
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!、