【问题标题】:Why jumpForce changes automatically in the inspector?为什么 jumpForce 在检查器中自动更改?
【发布时间】:2020-10-23 23:39:47
【问题描述】:

我正在使用 Unity 制作 2D 平台游戏。我希望我的游戏同时支持键盘和游戏手柄,因此我使用了新的输入系统和玩家输入组件。我的问题是当我按下跳跃按钮时,玩家没有正确跳跃。事实上,它在游戏开始时会跳跃,但之后它就不能再跳跃了,只是在低高度向上移动。我将游戏窗口最小化并按下播放键,发现第一次跳跃后,jumpForce值自动从300变为1。我想知道为什么会出现这种情况以及如何解决。提前致谢。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour, PlayerInputActions.IPlayerActions
 {
   private PlayerInputActions controls;     
   private Rigidbody2D rb;
   private Animator anim;
   private bool facingRight = true;
   private Vector2 moveInput;
  [SerializeField] private float jumpForce;
    float JumpPressedRemember = 0;
   
   [SerializeField] float JumpPressedRememberTime = 0.2f;
    float GroundedRemember = 0;
     
   [SerializeField]  float GroundedRememberTime = 0.25f;  
   [SerializeField]  float HorizontalAcceleration = 1;
   [SerializeField] [Range(0, 1)] float HorizontalDampingBasic = 0.5f;
   [SerializeField] [Range(0, 1)] float HorizontalDampingWhenStopping = 0.5f;
   [SerializeField] [Range(0, 1)] float HorizontalDampingWhenTurning = 0.5f;
   [SerializeField] [Range(0, 1)] float JumpHeight = 0.5f;

   private void Awake() 
   {
      controls = new PlayerInputActions();

      controls.Player.SetCallbacks(this);
    }
     void Start()
    {
      rb = GetComponent<Rigidbody2D>();
      anim = GetComponent<Animator>();       
}
   void PlayerInputActions.IPlayerActions.OnMove(InputAction.CallbackContext context)
   {
      moveInput = context.ReadValue<Vector2>();
          
   }

   void PlayerInputActions.IPlayerActions.OnJump(InputAction.CallbackContext context)
   {
       jumpForce = context.ReadValue<float>();
       switch (context.phase)
       {
            case InputActionPhase.Performed:
               this.Jump();
               break;
       }
   }

  void PlayerInputActions.IPlayerActions.OnShoot(InputAction.CallbackContext context)
  {
      
  }

   
   public void Jump()
   {
     rb.AddForce(Vector2.up * this.jumpForce, ForceMode2D.Force) ;
     

     
   }

  void FixedUpdate()
  {

   if(facingRight == false && moveInput.x > 0){
   
    Flip();
   
   }else if (facingRight == true && moveInput.x < 0){
    
    Flip();

   }
 }
    void Flip(){
    
    facingRight = !facingRight;
    Vector3 Scaler = transform.localScale;
    Scaler.x *= -1;
    transform.localScale = Scaler;
    
 }  
     

 void OnEnable()
 {
     controls.Enable();
 }
 
 void OnDisable()
 {
     controls.Disable();
 }
 void Update()
  {

   Vector2 GroundedBoxCheckPosition = (Vector2)transform.position + new Vector2(0, -0.01f);
   Vector2  GroundedBoxCheckScale = (Vector2)transform.localScale + new Vector2(-0.02f, 0);
   bool Grounded = Physics2D.OverlapBox(GroundedBoxCheckPosition, transform.localScale, 0);
    
    GroundedRemember -= Time.deltaTime;
    if (Grounded)
    {
      GroundedRemember = GroundedRememberTime;
    }

    JumpPressedRemember -= Time.deltaTime; 
   if (controls.Player.Jump.triggered)
   {
      JumpPressedRemember = JumpPressedRememberTime;
   }
     if (controls.Player.Jump.triggered)
        {
            if (rb.velocity.y > 0)
            {
                rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * JumpHeight);
            }
        }
    if ((JumpPressedRemember > 0) && (GroundedRemember > 0))
    {
       JumpPressedRemember = 0;
       GroundedRemember = 0;

      rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    }
     float HorizontalVelocity = rb.velocity.x;
      HorizontalVelocity += moveInput.x;
          
    
        if (Mathf.Abs(moveInput.x) < 0.01f)
            HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingWhenStopping, Time.deltaTime * 10f);
           else if (Mathf.Sign(moveInput.x) != Mathf.Sign(HorizontalVelocity))
           HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingWhenTurning, Time.deltaTime * 10f);
        else
            HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingBasic, Time.deltaTime * 10f);

        rb.velocity = new Vector2(HorizontalVelocity, rb.velocity.y);    
  }
}


  
    

【问题讨论】:

  • 我假设您的意思是 jumpForce 正在改变。您正在读取的值是否有可能在 0 和 1 之间标准化?您可以尝试将跳跃力设为常数并将其乘以该值。
  • 是的,我的意思是 jumpForce。您能否更具体一点或在代码中显示?

标签: c# unity3d


【解决方案1】:

我已经阅读了你的脚本,我不知道所有其他脚本的所有功能,但我想我找到了你的问题:

 void PlayerInputActions.IPlayerActions.OnJump(InputAction.CallbackContext context)
   {
       jumpForce = context.ReadValue<float>();  <-- HERE you set your jumpforce to something!!!
       switch (context.phase)
       {
            case InputActionPhase.Performed:
               this.Jump();
               break;
       }
   }

【讨论】:

  • 感谢您的回复。我试过你说的,但是当我在括号内写任何数字时,会出现错误提示“方法'ReadValue'没有重载需要1个参数[Assembly-CSharp]”。
  • 是的,我认为您需要完全删除该行并重试。
  • 如果我删除该行,播放器会跳出屏幕。这不是一个好主意。也许有些东西我需要改变,但不知道那是什么。
  • 这很奇怪,因为您使用的是 context.ReadLine 。但不幸的是我不知道问题出在哪里
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
  • 2020-01-29
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
相关资源
最近更新 更多