转弯时自行车运动感觉就像自行车在冰上
bike movement feels like the bike's on ice when turning
每当我转动我的自行车时,感觉就像在滑动。这是脚本
[SerializeField] float turn;
[SerializeField] float maxSpeed;
[SerializeField] GameObject bikeParts;
[SerializeField] float tilt = 20f;
Rigidbody rb;
float lastY;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float straightMov = Input.GetAxis("Vertical");
float horizontalMov = Input.GetAxis("Horizontal");
ControlStraightMov(straightMov);
ControlWeightTurning(horizontalMov);
lastY = transform.position.y;
}
private void ControlWeightTurning(float horizontalMov)
{
Vector3 bikePartsRot = bikeParts.transform.eulerAngles;
bikePartsRot.x = tilt * horizontalMov;
bikeParts.transform.eulerAngles = bikePartsRot;
transform.Rotate(0f, (turn * Time.deltaTime) * horizontalMov, 0f);
}
private void ControlStraightMov(float straightMov)
{
if (straightMov > 0)
{
rb.AddRelativeForce((accel * Time.deltaTime) * -straightMov, 0f, 0f);
}
else if (straightMov < 0)
{
rb.AddRelativeForce(((accel / 1.3f * Time.deltaTime) * -straightMov), 0f, 0f);
}
}
每当自行车加速时,就很难转动自行车,因为所施加的力使自行车沿与自行车所面对的方向不同的方向移动,我该如何施加它所面对的力之前在现在朝向的方向,这样才不会有滑动的感觉?
我试过解决这个问题,但我做不到,因为我对 unity 还很陌生。
how do I apply that force it was facing before on the direction it is facing now
不确定这是否合适,但您可能会这样做,例如
private void Update ()
{
...
rb.velocity = transform.forward * rb.velocity.magnitude;
}
这将保持当前速度并将其重定向到新的前进方向。
一般注意事项:每当涉及 Rigidbody
时,您不想通过 Transform
组件设置任何操作,而只能通过 Rigidbody
.
所以你不应该这样做
bikeParts.transform.eulerAngles = bikePartsRot;
transform.Rotate(0f, (turn * Time.deltaTime) * horizontalMov, 0f);
但两次都宁愿计算目标旋转并通过例如rb.MoveRotation
在 FixedUpdate
.
这看起来有点像
float horizontalMove;
void Update()
{
var straightMove = Input.GetAxis("Vertical");
// Get and store the input in Update
horizontalMove = Input.GetAxis("Horizontal");
ControlStraightMov(straightMove);
lastY = transform.position.y;
}
private void FixedUpdate ()
{
// Handle it in FixedUpdate
ControlWeightTurning(horizontalMove);
}
private void ControlWeightTurning(float horizontalMove)
{
var bikePartsRot = bikeParts.transform.eulerAngles;
bikePartsRot.x = tilt * horizontalMove;
// Calculate the new final rotation
var newRotation = Quaternion.Euler(bikePartsRot) * Quaternion.Euler(Vector3.up * (turn * Time.deltaTime) * horizontalMove));
// Apply it only via the Rigidbody
rb.MoveRotation(newRotation);
}
每当我转动我的自行车时,感觉就像在滑动。这是脚本
[SerializeField] float turn;
[SerializeField] float maxSpeed;
[SerializeField] GameObject bikeParts;
[SerializeField] float tilt = 20f;
Rigidbody rb;
float lastY;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float straightMov = Input.GetAxis("Vertical");
float horizontalMov = Input.GetAxis("Horizontal");
ControlStraightMov(straightMov);
ControlWeightTurning(horizontalMov);
lastY = transform.position.y;
}
private void ControlWeightTurning(float horizontalMov)
{
Vector3 bikePartsRot = bikeParts.transform.eulerAngles;
bikePartsRot.x = tilt * horizontalMov;
bikeParts.transform.eulerAngles = bikePartsRot;
transform.Rotate(0f, (turn * Time.deltaTime) * horizontalMov, 0f);
}
private void ControlStraightMov(float straightMov)
{
if (straightMov > 0)
{
rb.AddRelativeForce((accel * Time.deltaTime) * -straightMov, 0f, 0f);
}
else if (straightMov < 0)
{
rb.AddRelativeForce(((accel / 1.3f * Time.deltaTime) * -straightMov), 0f, 0f);
}
}
每当自行车加速时,就很难转动自行车,因为所施加的力使自行车沿与自行车所面对的方向不同的方向移动,我该如何施加它所面对的力之前在现在朝向的方向,这样才不会有滑动的感觉?
我试过解决这个问题,但我做不到,因为我对 unity 还很陌生。
how do I apply that force it was facing before on the direction it is facing now
不确定这是否合适,但您可能会这样做,例如
private void Update ()
{
...
rb.velocity = transform.forward * rb.velocity.magnitude;
}
这将保持当前速度并将其重定向到新的前进方向。
一般注意事项:每当涉及 Rigidbody
时,您不想通过 Transform
组件设置任何操作,而只能通过 Rigidbody
.
所以你不应该这样做
bikeParts.transform.eulerAngles = bikePartsRot;
transform.Rotate(0f, (turn * Time.deltaTime) * horizontalMov, 0f);
但两次都宁愿计算目标旋转并通过例如rb.MoveRotation
在 FixedUpdate
.
这看起来有点像
float horizontalMove;
void Update()
{
var straightMove = Input.GetAxis("Vertical");
// Get and store the input in Update
horizontalMove = Input.GetAxis("Horizontal");
ControlStraightMov(straightMove);
lastY = transform.position.y;
}
private void FixedUpdate ()
{
// Handle it in FixedUpdate
ControlWeightTurning(horizontalMove);
}
private void ControlWeightTurning(float horizontalMove)
{
var bikePartsRot = bikeParts.transform.eulerAngles;
bikePartsRot.x = tilt * horizontalMove;
// Calculate the new final rotation
var newRotation = Quaternion.Euler(bikePartsRot) * Quaternion.Euler(Vector3.up * (turn * Time.deltaTime) * horizontalMove));
// Apply it only via the Rigidbody
rb.MoveRotation(newRotation);
}