如何解决为对象添加相对于其局部轴的速度
所以我很难意识到如何为 rb 对象添加相对于其旋转的速度。这是我的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehavIoUr
{
private Rigidbody rb;
public CinemachineFreeLook camFreeLook;
public float speed = 5f;
private float refVeLocity;
void Start()
{
rb = GetComponent<Rigidbody>();
Cursor.visible = false;
}
void Update()
{
float xMovement = Input.GetAxis("Horizontal") * speed;
float zMovement = Input.GetAxis("Vertical") * speed;
if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{
rb.veLocity = new Vector3(xMovement,rb.veLocity.y,zMovement); // Here is the problem. It moves the player relative to the world axis,but im trying to move it relative to it's local axis
}
if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{
float rotationY = Mathf.SmoothdampAngle(transform.eulerAngles.y,camFreeLook.m_XAxis.Value,ref refVeLocity,0.1f);
transform.eulerAngles = new Vector3(transform.eulerAngles.x,rotationY,transform.eulerAngles.z);
}
}
}
解决方法
使用transform.TransformDirection
将方向从局部空间转换为世界空间:
rb.velocity = transform.TransformDirection(
new Vector3(xMovement,rb.velocity.y,zMovement));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。