简体   繁体   English

如何让我的孩子 object 围绕父母正确旋转?

[英]How to make my child object correctly rotate around the parent?

I previously had the below code, which I used to orientate my object towards where the right analog stick on my controller was (using the unity input system).我以前有下面的代码,我用它来将我的 object 定位到我的 controller 上正确的模拟摇杆所在的位置(使用统一输入系统)。

public void OnAim(InputAction.CallbackContext context)
{
    aimDir = context.ReadValue<Vector2>();
}

void HandleRotation()
{
    if (aimDir.magnitude >= 0.5f)
    {
        var aimAngle = Mathf.Atan2(aimDir.y, aimDir.x) * Mathf.Rad2Deg - 90f;
        rb.rotation = aimAngle;
    }
}

This worked by rotating the entire parent, however I now want to keep the parent object rotation fixed whilst just rotating the child object around it.这是通过旋转整个父级来实现的,但是我现在想保持父级 object 旋转固定,同时只围绕它旋转子级 object。 I tried to rewrite the code:我试图重写代码:

void HandleRotation()
{
    if (aimDir.magnitude >= 0.5f)
    {
        var aimAngle = Mathf.Atan2(aimDir.y, aimDir.x) * Mathf.Rad2Deg - 90f;
        shootPoint.transform.RotateAround(transform.position, new Vector3(0, 0, 1), aimAngle);
    }
}

But this makes it constantly spin rather than just smoothly rotating towards where my right analog stick is facing.但这使得它不断旋转,而不是平稳地朝着我的右摇杆所面对的方向旋转。 How do I fix this?我该如何解决?

Fistly, in this:首先,在这个:

var aimAngle = Mathf.Atan2(aimDir.y, aimDir.x) * Mathf.Rad2Deg - 90f;

Your aimAngle is not rad but degree so, i think you should use你的 aimAngle 不是弧度而是度数,所以我认为你应该使用

rb.rotation = Qaternion.Eualar(aimAngle); 

instead of代替

rb.rotation = aimAngle;

In your question, you could create an empty GameObject, put it in your child object(which you want to rotate)'s parent then put your child object in this empty object. Then rotate this empty object. This way, you have a rotating child object seems like parent turning and it is turning around it.在你的问题中,你可以创建一个空的游戏对象,把它放在你的子对象(你想旋转)的父对象中,然后把你的孩子 object 放在这个空的 object 中。然后旋转这个空的 object。这样,你有一个旋转child object 看起来像 parent turning 并且正在绕着它转。 Also, you awaided to rotate parent too.此外,您也等待轮换父母。

This can be done with Vector3.RotateTowards() and localPosition .这可以通过Vector3.RotateTowards()localPosition来完成。 Attach the shootPoint game object to a parent game object, attach a script with the following code to the parent object, and give the script a reference to shootPoint .shootPoint游戏 object 附加到父游戏 object,将具有以下代码的脚本附加到父游戏 object,并为脚本提供对shootPoint的引用。

public Transform shootPoint;
public float distance = 2f;
public float speed = 5f;

private Vector3 _pos;
private Vector2 _aimDir;

public void OnAim(InputAction.CallbackContext context) =>
    aimDir = context.ReadValue<Vector2>();

private void Update()
{
    if (_aimDir.magnitude >= float.Epsilon) 
        _pos = new Vector3(_aimDir.x, 0f, _aimDir.y).normalized;

    shootPoint.localPosition =
            Vector3.RotateTowards(
                shootPoint.localPosition, 
                _pos * distance, 
                speed * Time.deltaTime, 
                0f);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM