如何围绕另一个对象围绕圆圈移动某个对象?

时间:2018-04-05 23:09:18

标签: c# unity3d

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

public class TargetBehaviour : MonoBehaviour
{
    // Add this script to Cube(2)
    [Header("Add your turret")]
    public GameObject Turret;//to get the position in worldspace to which this gameObject will rotate around.

    [Header("The axis by which it will rotate around")]
    public Vector3 axis;//by which axis it will rotate. x,y or z.

    [Header("Angle covered per update")]
    public float angle; //or the speed of rotation.

    public float upperLimit, lowerLimit, delay;// upperLimit & lowerLimit: heighest & lowest height; 
    private float height, prevHeight, time;//height:height it is trying to reach(randomly generated); prevHeight:stores last value of height;delay in radomness; 

    // Update is called once per frame
    void Update()
    {
        //Gets the position of your 'Turret' and rotates this gameObject around it by the 'axis' provided at speed 'angle' in degrees per update 
        transform.RotateAround(Turret.transform.position, axis, angle);
        time += Time.deltaTime;
        //Sets value of 'height' randomly within 'upperLimit' & 'lowerLimit' after delay 
        if (time > delay)
        {
            prevHeight = height;
            height = Random.Range(lowerLimit, upperLimit);
            time = 0;
        }
        //Mathf.Lerp changes height from 'prevHeight' to 'height' gradually (smooth transition)  
        transform.position = new Vector3(transform.position.x, Mathf.Lerp(prevHeight, height, time), transform.position.z);
    }
}

一般来说,如果我在变量上将轴x,y,z设置为1,1,1,那么它的工作问题是:

角度设置为1上限为50下限为2,延迟为2。

然后物体围绕另一个物体形成一个圆圈,但有时当物体越来越高时,物体越大,物体越大,然后当物体越低时,圆圈越小。

如何保持圆半径静止?

主要目标是将对象围绕另一个对象以随机高限制移动,例如2和50,但我希望始终保持相同的半径。现在半径根据高度而变化。

1 个答案:

答案 0 :(得分:2)

当你不断向上移动物体时,如果你想让旋转半径保持不变,那么旋转轴必须是正确的,即 - Vector3.upnew Vector3(0, 1, 0)

相关问题