C# 墙壁跳跃高度随每次跳跃而降低
解决-我最终删除了C# 墙壁跳跃高度随每次跳跃而降低,c#,unity3d,C#,Unity3d,解决-我最终删除了playerController.SimpleMove并让playerController.Move控制玩家的所有移动 所以我一直在努力让我的角色墙在墙与墙之间跳跃。我已经能够使它工作,但是,在每一次成功的墙壁跳跃后,下一次跳跃的高度降低。这就达到了它开始向下跳墙的程度。我不知道它为什么会这样。每次按下空格键之前,我都会将运动矢量3重置为零,然后将正确的值重新应用于跳跃。我甚至通过控制台查看了垂直变化,moveDirection和moveAmount都没有得到足够大的值变化来实
playerController.SimpleMove
并让playerController.Move
控制玩家的所有移动
所以我一直在努力让我的角色墙在墙与墙之间跳跃。我已经能够使它工作,但是,在每一次成功的墙壁跳跃后,下一次跳跃的高度降低。这就达到了它开始向下跳墙的程度。我不知道它为什么会这样。每次按下空格键之前,我都会将运动矢量3重置为零,然后将正确的值重新应用于跳跃。我甚至通过控制台查看了垂直变化,moveDirection和moveAmount都没有得到足够大的值变化来实现这一点
因为我只是尝试一下,下面是所有影响玩家的代码
public float rotateSpeed = 3.0f;
public float walkSpeed = 5.0f;
public float runSpeed = 15.0f;
public float jumpSpeed = 10.0f;
public float acceleration = .05f;
public bool running = false;
public bool jumping = false;
public bool falling = false;
public bool onWall = false;
public bool wallJumping = false;
public bool stay = false;
private CharacterController playerController;
private Animator playerAnimator;
private float speed;
private Vector3 moveAmount;
private float animationSpeed;
private float currentSpeed;
private float targetSpeed;
private float moveSpeed;
float gravity = 10f;
private Vector3 moveDirection = Vector3.zero;
// Use this for initialization
void Start () {
playerController = GetComponent<CharacterController>();
playerAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
if (onWall)
{
playerAnimator.SetBool("Wall Holding", true);
jumping = false;
}
else
{
playerAnimator.SetBool("Wall Holding", false);
}
if (stay)
{
moveDirection = Vector3.zero;
moveAmount = Vector3.zero;
}
#region Player Controls
if (Input.GetButton("Vertical") && !wallJumping)
{
moveSpeed = (Input.GetKey(KeyCode.LeftShift)) ? runSpeed : walkSpeed;
moveAmount = transform.TransformDirection(Vector3.forward);
}
else
{
moveSpeed = 0;
moveAmount = Vector3.zero;
}
if (playerController.isGrounded){
moveDirection = Vector3.zero;
jumping = false;
wallJumping = false;
onWall = false;
stay = false;
}
else
{
if (!stay)
onWall = false;
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (playerController.isGrounded)
{
moveDirection.y = jumpSpeed;
jumping = true;
}
else if (onWall)
{
moveDirection = Vector3.zero;
moveAmount = Vector3.zero;
wallJumping = true;
moveDirection += transform.forward * jumpSpeed * 2f;
moveDirection += transform.up * jumpSpeed * 2f;
stay = false;
}
}
if (Input.GetKeyDown(KeyCode.F))
{
if (playerAnimator.GetBool("fight"))
{
playerAnimator.SetBool("fight", false);
}
else
{
playerAnimator.SetBool("fight", true);
}
}
#endregion Player Controls
moveDirection.y -= gravity * Time.deltaTime;
Debug.Log(moveDirection.x);
targetSpeed = Input.GetAxis("Vertical") * moveSpeed;
currentSpeed = SpeedFactor(targetSpeed, currentSpeed, acceleration);
if (currentSpeed > walkSpeed)
running = true;
else
running = false;
playerAnimator.SetFloat("movespeed", currentSpeed);
if (!stay)
{
playerController.SimpleMove(currentSpeed * moveAmount);
playerController.Move(moveDirection * Time.deltaTime);
}
}
private float SpeedFactor (float targetSpeed, float currentSpeed, float dilation) {
if ( currentSpeed < targetSpeed) {
currentSpeed += dilation;
if ( currentSpeed > targetSpeed) {
currentSpeed = targetSpeed;
}
}
else if ( currentSpeed > targetSpeed) {
currentSpeed -= dilation;
if ( currentSpeed < targetSpeed) {
currentSpeed = targetSpeed;
}
}
return currentSpeed;
}
void OnControllerColliderHit (ControllerColliderHit hit) {
if (hit.gameObject.tag == "JumpingWall" && !playerController.isGrounded && !onWall && (jumping || wallJumping))
{
onWall = true;
transform.Rotate(0,180,0);
stay = true;
}
}
public float rotateSpeed=3.0f;
公共浮子行走速度=5.0f;
公共浮子运行速度=15.0f;
公共浮子跳跃速度=10.0f;
公众浮标加速度=.05f;
公共bool running=false;
公共布尔跳跃=错误;
公共布尔下降=错误;
公共bool onWall=假;
公共布尔值=假;
公共布尔停留=假;
专用字符控制器playerController;
私人动画师;
私人浮动速度;
私有向量3移动量;
私人浮动动画速度;
私人浮动速度;
私人浮动目标速度;
私人浮动速度;
浮子重力=10f;
专用矢量3移动方向=矢量3.0;
//用于初始化
无效开始(){
playerController=GetComponent();
playerAnimator=GetComponent();
}
//每帧调用一次更新
无效更新(){
transform.Rotate(0,Input.GetAxis(“水平”)*rotateSpeed,0);
如果(在墙上)
{
playerImator.SetBool(“持墙”,真);
跳跃=错误;
}
其他的
{
playerImator.SetBool(“持墙”,假);
}
如果(留下)
{
移动方向=矢量3.0;
moveAmount=Vector3.0;
}
#区域玩家控制
if(Input.GetButton(“垂直”)和&!wallJumping)
{
moveSpeed=(Input.GetKey(KeyCode.LeftShift))?运行速度:walkSpeed;
moveAmount=transform.TransformDirection(Vector3.forward);
}
其他的
{
移动速度=0;
moveAmount=Vector3.0;
}
如果(playerController.isGrounded){
移动方向=矢量3.0;
跳跃=错误;
墙跳=假;
onWall=false;
停留=假;
}
其他的
{
如果(!停留)
onWall=false;
}
if(Input.GetKeyDown(KeyCode.Space))
{
如果(playerController.isGrounded)
{
移动方向。y=跳跃速度;
跳跃=正确;
}
else if(墙上)
{
移动方向=矢量3.0;
moveAmount=Vector3.0;
wallJumping=正确;
moveDirection+=变换向前*跳跃速度*2f;
moveDirection+=transform.up*jumpSpeed*2f;
停留=假;
}
}
if(Input.GetKeyDown(KeyCode.F))
{
if(playerAnimator.GetBool(“战斗”))
{
playerAnimator.SetBool(“战斗”,假);
}
其他的
{
playerAnimator.SetBool(“战斗”,正确);
}
}
#端域播放器控件
moveDirection.y-=重力*时间增量;
Log(moveDirection.x);
targetSpeed=输入。GetAxis(“垂直”)*移动速度;
当前速度=速度系数(目标速度、当前速度、加速度);
如果(当前速度>行走速度)
运行=真;
其他的
运行=错误;
playerImator.SetFloat(“移动速度”,currentSpeed);
如果(!停留)
{
playerController.SimpleMove(当前速度*移动量);
playerController.Move(moveDirection*Time.deltaTime);
}
}
专用浮点速度因子(浮点目标速度、浮点当前速度、浮点扩展){
if(当前速度<目标速度){
电流速度+=膨胀;
如果(当前速度>目标速度){
当前速度=目标速度;
}
}
否则如果(当前速度>目标速度){
电流速度-=膨胀;
if(当前速度<目标速度){
当前速度=目标速度;
}
}
回流速度;
}
控件碰撞无效(控件碰撞无效){
如果(hit.gameObject.tag==“JumpingWall”&&!playerController.isGrounded&&!onWall&&&(jumping | | wallJumping))
{
onWall=true;
变换.旋转(0180,0);
停留=正确;
}
}
最简单的解释是,transform.forward
和/或transform.up
向量随着时间的推移开始偏离您的期望。这就解释了当你应用下面的行时,它最终看起来是如何“向后”跳跃的。在跳线与墙碰撞后(在下一次跳转之前),您确定这些向量的方向与预期一致吗
谢谢你的回复。至于你的问题,是的,我相当肯定他们是。我甚至将两行都设置为=而不是+=并且出现了相同的结果。我做了一些进一步的测试,似乎高度似乎是向后跳跃的一个因素,但我也不知道为什么会这样。与
+=
相比=
不应该有什么区别,因为在这两个作业之前,您将移动方向
归零。您可以尝试替换moveDirection+=transform.up*jumpSpeed*2f代码>带有moveDirection.y=跳跃速度*2f代码>(最后一个赋值语句)以查看它的区别。.y
向量应该是不变的,而transform.up
向量将取决于跳线的方向。在这两条赋值语句之后,我将记录/检查moveDirection
的所有3个组件。然后,您将看到如何将力分配给每个不变的3D向量
moveDirection += transform.forward * jumpSpeed * 2f;
moveDirection += transform.up * jumpSpeed * 2f;