C# 玩家在相机方向移动,能够在没有动画的情况下跳跃
我正在尝试创建一个代码,允许我的播放器跟随相机方向,我无法实现快速跳跃,而不是基于动画 我看过很多youtube视频,但没有一个视频提供了一个带第三人称摄像头的跳跃机械师。唯一一个有相似之处的是第一人称相机,但跳跃是基于动画的,我所做的那种水平力学让人难以忍受 这是我的玩家移动代码C# 玩家在相机方向移动,能够在没有动画的情况下跳跃,c#,unity3d,C#,Unity3d,我正在尝试创建一个代码,允许我的播放器跟随相机方向,我无法实现快速跳跃,而不是基于动画 我看过很多youtube视频,但没有一个视频提供了一个带第三人称摄像头的跳跃机械师。唯一一个有相似之处的是第一人称相机,但跳跃是基于动画的,我所做的那种水平力学让人难以忍受 这是我的玩家移动代码 using System.Collections; using System.Collections.Generic; using UnityEngine; public class OldPlayerMoveme
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OldPlayerMovementScript : MonoBehaviour
{
[SerializeField] private string mouseXInputName;
[SerializeField] private float mouseSensitivity;
CharacterController controller;
public float speed = 5;
float verticalVelocity;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
float x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
float z = Input.GetAxis("Vertical") * speed * Time.deltaTime;
float mouseX = Input.GetAxis(mouseXInputName) * mouseSensitivity * Time.deltaTime;
transform.Rotate(Vector3.up * mouseX);
if (controller.isGrounded)
{
verticalVelocity = -1 * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.V))
{
verticalVelocity = 20 * Time.deltaTime;
}
}
else
{
verticalVelocity -= 1 * Time.deltaTime;
}
Vector3 moveDelta = new Vector3(x, verticalVelocity, z);
controller.Move(moveDelta);
}
}
This is my Camera Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerLook : MonoBehaviour
{
[SerializeField] private string mouseXInputName, mouseYInputName;
[SerializeField] private float mouseSensitivity;
//[SerializeField] private Transform playerBody;
private float xAxisClamp;
private void Awake()
{
LockCursor();
xAxisClamp = 0.0f;
}
private void LockCursor()
{
Cursor.lockState = CursorLockMode.Locked;
}
private void Update()
{
CameraRotation();
}
private void CameraRotation()
{
float mouseX = Input.GetAxis(mouseXInputName) * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis(mouseYInputName) * mouseSensitivity * Time.deltaTime;
xAxisClamp += mouseY;
if(xAxisClamp > 60.0f)
{
xAxisClamp = 60.0f;
mouseY = 0.0f;
ClampXAxisRotationToValue(300.0f);
}
else if (xAxisClamp < -60.0f)
{
xAxisClamp = -60.0f;
mouseY = 0.0f;
ClampXAxisRotationToValue(60.0f);
}
transform.Rotate(Vector3.left * mouseY);
// playerBody.Rotate(Vector3.up * mouseX);
}
private void ClampXAxisRotationToValue(float value)
{
Vector3 eulerRotation = transform.eulerAngles;
eulerRotation.x = value;
transform.eulerAngles = eulerRotation;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类OldPlayerMovementScript:MonoBehavior
{
[SerializeField]私有字符串mouseXInputName;
[SerializeField]专用浮点鼠标灵敏度;
字符控制器;
公共浮动速度=5;
浮动垂直性;
void Start()
{
控制器=GetComponent();
}
无效更新()
{
float x=Input.GetAxis(“水平”)*速度*Time.deltaTime;
float z=Input.GetAxis(“垂直”)*速度*Time.deltaTime;
float mouseX=Input.GetAxis(mouseXInputName)*mouseSensitivity*Time.deltaTime;
变换。旋转(矢量3.up*mouseX);
if(controller.isground)
{
verticalVelocity=-1*Time.deltaTime;
if(Input.GetKeyDown(KeyCode.V))
{
垂直延伸率=20*Time.deltaTime;
}
}
其他的
{
垂直延伸率-=1*Time.deltaTime;
}
Vector3 moveDelta=新的Vector3(x,垂直度,z);
控制器。移动(移动增量);
}
}
这是我的相机代码
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类PlayerLook:单一行为
{
[SerializeField]私有字符串MouseInputName,MouseInputName;
[SerializeField]专用浮点鼠标灵敏度;
//[SerializeField]专用转换播放器主体;
私人浮子夹;
私人空间
{
锁定光标();
xAxisClamp=0.0f;
}
私有void锁游标()
{
Cursor.lockState=CursorLockMode.Locked;
}
私有void更新()
{
摄影机旋转();
}
专用无效摄影机旋转()
{
float mouseX=Input.GetAxis(mouseXInputName)*mouseSensitivity*Time.deltaTime;
float mouseY=Input.GetAxis(mouseYInputName)*mouseSensitivity*Time.deltaTime;
xAxisClamp+=鼠标;
如果(xAxisClamp>60.0f)
{
xAxisClamp=60.0f;
mouseY=0.0f;
卡箍X轴旋转值(300.0f);
}
否则如果(xAxisClamp<-60.0f)
{
xAxisClamp=-60.0f;
mouseY=0.0f;
夹具X轴旋转至数值(60.0f);
}
transform.Rotate(Vector3.left*mouseY);
//playerBody.旋转(Vector3.up*mouseX);
}
私有void ClampXAxisRotationToValue(浮点值)
{
Vector3 eulerotation=transform.eulerAngles;
eulerotation.x=值;
transform.eulerAngles=eulerotation;
}
}
目前,我可以沿着摄影机的方向移动角色,但无论我如何无法实现跳跃机制,我都将玩家身体的旋转移动到x轴,因为在使用影响玩家变换的平台时,我遇到了身体变形的问题。我正在制作的游戏是一个平板游戏,因此跳跃是必不可少的,但无法转向新方向会使游戏过于线性。可能需要添加一些标签,因为这是为。。我想是统一吧?