Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使火箭围绕它旋转';在统一中是底部而不是中间?_C#_Unity3d - Fatal编程技术网

C# 如何使火箭围绕它旋转';在统一中是底部而不是中间?

C# 如何使火箭围绕它旋转';在统一中是底部而不是中间?,c#,unity3d,C#,Unity3d,我该如何让火箭播放器控制器围绕底部旋转呢 这是我到目前为止的代码(我知道它有问题,现在只是一个概念证明) 使用系统; 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共级火箭:单一行为 { 刚体刚体; void Start() { 刚体=GetComponent(); } 无效更新() { ProcessInput(); } 私有void ProcessInput() { if(Input.GetKey(KeyCode.Space))

我该如何让火箭播放器控制器围绕底部旋转呢

这是我到目前为止的代码(我知道它有问题,现在只是一个概念证明)

使用系统;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共级火箭:单一行为
{
刚体刚体;
void Start()
{
刚体=GetComponent();
}
无效更新()
{
ProcessInput();
}
私有void ProcessInput()
{
if(Input.GetKey(KeyCode.Space))
{
刚体.AddRelativeForce(矢量3.up);
}
if(Input.GetKey(KeyCode.A)| Input.GetKey(KeyCode.LeftArrow))
{
如果(!Input.GetKey(KeyCode.D)和&!Input.GetKey(KeyCode.RightArrow))
{
旋转(新向量3(0,0,2));
}
}
if(Input.GetKey(KeyCode.D)| Input.GetKey(KeyCode.RightArrow))
{
if(!Input.GetKey(KeyCode.A)和&!Input.GetKey(KeyCode.LeftArrow))
{
旋转(新向量3(0,0,-2));
}
}
}
}
好消息,Unity

但是!更有用的是,它有:

我很确定这正是你想要的

(顺便说一句,要想知道你旋转的向量是什么并不容易。你必须对叉积和其他向量操作有很好的感觉。Unity就是这样让人恼火-一开始似乎很容易使用,但你真的需要对物理学有很好的掌握(我是说牛顿物理学)向量和四元数的每一个方面都有助于真正制作游戏——这很难。)


一个提示:你可以看到你是如何增加力量来移动它的。尝试添加扭矩(
AddTorque
)来转动它

如果你有一个
刚体
,你不应该通过
变换混搭动作
!这破坏了物理和碰撞检测,并导致奇怪的行为


而是根据需要设置重心

然后还是

虽然一般来说,使用每秒角度更容易,例如

rigidBody.MoveRotation(rigidbody.rotation * Quaternion.Euler(0, 0, -45 * Time.deltaTime));
Time.deltaTime
是自上次物理更新以来经过的时间,因此这可以确保您精确地以
-45°/秒的速度旋转


或者查看->您可以简单地将网格放在父对象下,而不是将该父对象用作刚体和移动脚本等。因此,您可以创建相对于父对象的任何偏移


是的,你也可以自己重新计算网格顶点的位置,以满足你的需要,但通常父母的方法已经足够了;)

在谷歌上搜索“unity change pivot point”比在那里创建一个全新的问题要容易得多……谢谢!那正是我要找的!哈哈,不用担心@YuvalAmir-很幸运,但要小心这些!千万不要通过
变换
组件将物理(刚体)与任何变换混合在一起,因为这会破坏物理和碰撞检测,并且通常表现得异常正确!注意答案中的最后一句话,@YuvalAmir
private void ProcessInput()
{
    if (Input.GetKey(KeyCode.Space))
    {
        rigidBody.AddRelativeForce(Vector3.up);
    }

    if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
    {
        if (!Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.RightArrow))
        {
            turnLeft = true;
        }
    }

    if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
    {
        if (!Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.LeftArrow))
        {
            turnRight = true;
        }
    }
}

private bool turnLeft;
private bool turnRight;

private void FixedUpdate ()
{
    if(turnLeft)
    {
        turnLeft = false;
        rigidbody.MoveRotation(rigidBody.MoveRotation(rigidbody.rotation * Quaternion.Euler(0, 0, 2));
    }

    if(turnRight)
    {
        turnRight = false;
        rigidBody.MoveRotation(rigidBody.MoveRotation(rigidbody.rotation * Quaternion.Euler(0, 0, -2));
    }
}
rigidBody.MoveRotation(rigidbody.rotation * Quaternion.Euler(0, 0, -45 * Time.deltaTime));