围绕一个点旋转对象

时间:2019-07-09 00:00:35

标签: unity3d

我是Unity的新手。因此,仅出于实验目的,我想通过将矩形附加到圆上来创建佳能,然后按向上箭头键,佳能会改变发射角度。所以我有一个矩形对象,它是圆的子对象。然后,我用C#为圆对象创建了一个脚本。

enter image description here

这是我拥有的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private float rotation = 0f;
    private float timeValue = 0.0f;
    public GameObject wheele;
    private float xMin = -1.0f, xMax = 1.0f;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            if (rotation >= -90)
                transform.Rotate(new Vector3(0.0f, 0.0f, rotation));

            rotation -= 2;
            Mathf.Clamp(rotation, -90.0f, 0);
        }
        if(Input.GetKeyDown(KeyCode.DownArrow))
        {
            if (rotation >= -90)
                transform.RotateAround(wheele.transform.position, Vector3.up,20);

            rotation += 2;
            Mathf.Clamp(rotation, -90.0f, 0);
        }


    }
}

我尝试了两种转换。旋转方法,但它绕矩形的中心旋转。但是我们需要矩形随轴(圆心)旋转。

2 个答案:

答案 0 :(得分:1)

您在问如何更改枢轴点,对吗?制作一个空的游戏对象并拖动下面的大炮使其成为孩子,然后将大炮拖动到您认为合适的位置并旋转空的游戏对象而不是大炮,这几乎只是改变了枢轴点

答案 1 :(得分:1)

主要有两种方法。

  1. 更新您的模型以将轴心点精确设置为您希望其旋转的位置。 This is how you would do this using blender。但这实际上需要您为佳能创建一个模型(即使它与您使用Unity原语的模型一样简单)

  2. 创建一个父GameObject,然后旋转它。您可以在此GameObject内偏移大炮,以补偿在模型上设置的枢轴点。

相关问题