简体   繁体   English

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

[英]How can I move some object in circle around another object?

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);
    }
}

In general it's working the problem is for example if I set the axis x,y,z to 1,1,1 on the variable: axis 总的来说,它起作用了,例如,如果我在变量:axis上将x,y,z轴设置为1,1,1

And Angle set to 1 Upper limit to 50 Lower limit to 2 and delay to 2. 角度设置为1上限设置为50下限设置为2,延迟设置为2。

Then the object is making a circle around the other object but sometimes when the object is getting higher the most he get higher the object making a bigger circle and then when the object is getting lower the circle is smaller. 然后,该对象在另一个对象上绕一个圆,但是有时,当对象变得越来越高时,他越靠近对象,则形成一个更大的圆;然后,当对象变得越低时,该圆圈就会变小。

How can I make it to keep the circle radius static ? 如何使其保持圆半径不变?

The main goal is to move the object in circles around another object in random highs limits for example 2 and 50 but I want to keep the same radius all the time. 主要目标是以随机上限(例如2和50)在另一个对象周围绕圈移动对象,但我想一直保持相同的半径。 Now the radius is changing by depending on the height. 现在,半径根据高度而变化。

当您不断向上移动对象时,如果希望旋转半径保持恒定,则旋转轴必须垂直,即Vector3.upnew Vector3(0, 1, 0) Vector3.up new Vector3(0, 1, 0)

暂无
暂无

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

相关问题 如何使用 linerenderer 绘制一个圆圈并将其作为 object 的子 object 的圆圈应该画出来? - How can I draw a circle using linerenderer and make it the circle as object child of the object is should draw around? 如何将缓慢平滑的对象移动为另一个对象的子对象? - How can I move slowly smooth object to be child of another object? 如何使用 LineRenderer 根据 object 周围的半径大小绘制圆? - How can I use LineRenderer to draw a circle depending on radius size around an object? 如何围绕特定的 object 和/或围绕鼠标单击 position 画一个圆圈? - How to draw a circle around specific object and/or around the mouse click position? 在 object 周围实例化六边形 - Instantiating hexagons in a circle around an object 如何在每个实例化对象之间添加间隙? 预制件应在绘制的圆上实例化并在其中移动:if(moveInCircles) - How can I add a gap between each Instantiated object ? The prefabs should be Instantiated on the drawn circle and move inside: if (moveInCircles) 如何倾斜某个物体? - How can I move an object at an angle? 我如何将对象移动到某个随机位置,然后在到达新位置时旋转并移动到新的随机位置? - How can i move object to some random position then rotate when reached to the new position and move to new random position? 如何在unity3d中围绕对象绘制圆圈? - How to draw circle around an object in unity3d? Unity 5:如何使一个对象移动到另一个对象? - Unity 5: How to make an object move to another object?
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM