我的方块在跳跃时应该旋转 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);
}
}
}
我尝试了这段代码,我希望它能够工作,但是跳跃高度非常低,因为它与旋转相冲突
是否需要旋转整个刚体而不是仅旋转精灵? 否则,旋转速度矢量并在正方形旋转时回复它可能是明智之举。