速度和旋转问题

problems with velocity and rotation

提问人:jude 提问时间:11/7/2023 最后编辑:jude 更新时间:11/7/2023 访问量:40

问:

我的方块应该在跳跃时旋转 90 度。但是旋转会干扰跳跃高度。起初我尝试实际旋转,现在我正在尝试动画。我试图改变惯性值,但意识到它是只读的。这是我的代码:

using System.Collections.Generic;
using UnityEngine;



public class MoveScript : MonoBehaviour
{
    private bool isGrounded = true;
    public float walkSpeed = 5;
    public float jumpHeight = 7;
    public Rigidbody2D myRigidBody;
    public Transform myTransform;
    public Animator myAnimator;
    //myRigidBody.inertia = 4;
    //private float rotationStep = -30;
    // Start is called before the first frame update
    void Start()
    {

    }
    void OnCollisionEnter2D(Collision2D col)
    {
        isGrounded = true;

    }
    void OnCollisionExit2D(Collision2D col)
    {
        isGrounded = false;

    }
      
    void Update()
    {
        myRigidBody.rotation = 0; //i set the rotation to 0 so that if the player jumps on the corner of a block, they aren't sent in another direction.
        myTransform.Translate(Vector2.right * walkSpeed * Time.deltaTime);
        if (Input.GetMouseButtonDown(0) && isGrounded == true)
        {
            Jump();
            
            myAnimator.SetTrigger("RotateTrigger");
        }      
        void Jump()
        {
            myRigidBody.velocity = new Vector2(0, jumpHeight);
        }
       }
}

我尝试了这个代码,我希望它能工作,但是跳跃高度真的很低,因为它与旋转冲突

C# unity-游戏引擎 2D

评论

0赞 jude 11/7/2023
如果有任何混淆,我的意思是正方形,而不是立方体
0赞 derHugo 11/7/2023
myTransform.Translate(Vector2.right * walkSpeed * Time.deltaTime);...这发生在局部空间(因此取决于旋转)并且完全干扰了物理学......你永远不应该混淆运动 via 和RigidbodyTransform
0赞 Elec1 11/7/2023
@jude当你指的是正方形而不是立方体时,你应该编辑你的问题。
0赞 derHugo 11/7/2023
另外,为什么你的本地函数嵌套在其中?如果它是单行,你可以简单地用你的跳线立即替换。此外,在大多数情况下,对于跳跃,您希望保持 X 轴上的当前速度,并且只覆盖 Y 值void JumpUpdateJump();

答:

0赞 Alexanderb 11/7/2023 #1

我有几点想法:

  • 创建另一个刚体作为当前刚体的父体,并使当前体始终具有相同的位置,然后旋转当前刚体。
  • 旋转速度矢量,并在方块旋转时回复它。
  • 使用“添加力”而不是设置速度

评论

0赞 jude 11/7/2023
我将如何只旋转精灵,而不是刚体
0赞 Alexanderb 11/7/2023
我之前提出的旋转精灵的想法应该可以工作两个,但似乎是一个肮脏的修复。