C# 相对于对象的局部轴向对象添加速度
所以我很难意识到我怎样才能增加rb物体相对于其旋转的速度。这是我的剧本:C# 相对于对象的局部轴向对象添加速度,c#,unity3d,rigid-bodies,C#,Unity3d,Rigid Bodies,所以我很难意识到我怎样才能增加rb物体相对于其旋转的速度。这是我的剧本: using System.Collections; using System.Collections.Generic; using UnityEngine; using Cinemachine; [RequireComponent(typeof(Rigidbody))] public class PlayerController : MonoBehaviour { private Rigidbo
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);
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用电影机;
[RequiredComponent(类型)(刚体))]
公共类玩家控制器:单行为
{
私人刚体;
公共影院;Freelook camFreeLook;
公共浮子速度=5f;
私人浮动速度;
void Start()
{
rb=GetComponent();
Cursor.visible=false;
}
无效更新()
{
float xMovement=Input.GetAxis(“水平”)*速度;
float zMovement=Input.GetAxis(“垂直”)*速度;
if(Input.GetButton(“水平”)| | Input.GetButton(“垂直”))
{
rb.velocity=newvector3(xMovement,rb.velocity.y,zMovement);//问题出在这里。它相对于世界轴移动玩家,但我试图相对于本地轴移动玩家
}
if(Input.GetButton(“水平”)| | Input.GetButton(“垂直”))
{
浮动旋转y=数学平滑阻尼角(transform.eulerAngles.y,camFreeLook.m_XAxis.Value,参考速度,0.1f);
transform.eulerAngles=新矢量3(transform.eulerAngles.x,rotationY,transform.eulerAngles.z);
}
}
}
如果有人知道如何修复此问题,请添加解释。谢谢 用于将方向从局部空间转换为世界空间:
rb.velocity = transform.TransformDirection(
new Vector3(xMovement, rb.velocity.y, zMovement));
但是这对于
Y
组件来说不是不正确的,因为它已经在世界空间中了吗?;)@derHugo在很多情况下是的,但如果我们能确定局部y与全局y的方向相同,我们可以做一些欺骗;)一般来说,不应该像这样旋转对象。1) 不要动态更改2)不要将刚体操纵与变换混合使用。而不是在固定更新中使用