提问人:jude 提问时间:11/7/2023 最后编辑:jude 更新时间:11/7/2023 访问量:40
速度和旋转问题
problems with velocity and rotation
问:
我的方块应该在跳跃时旋转 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);
}
}
}
我尝试了这个代码,我希望它能工作,但是跳跃高度真的很低,因为它与旋转冲突
答:
0赞
Alexanderb
11/7/2023
#1
我有几点想法:
- 创建另一个刚体作为当前刚体的父体,并使当前体始终具有相同的位置,然后旋转当前刚体。
- 旋转速度矢量,并在方块旋转时回复它。
- 使用“添加力”而不是设置速度
评论
0赞
jude
11/7/2023
我将如何只旋转精灵,而不是刚体
0赞
Alexanderb
11/7/2023
我之前提出的旋转精灵的想法应该可以工作两个,但似乎是一个肮脏的修复。
评论
myTransform.Translate(Vector2.right * walkSpeed * Time.deltaTime);
...这发生在局部空间(因此取决于旋转)并且完全干扰了物理学......你永远不应该混淆运动 via 和Rigidbody
Transform
void Jump
Update
Jump();