0
\$\begingroup\$
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.normalized, 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);
    }
}

Now it's rotating in a constant radius. I set the variable axis to 0,1,0

But if I want the object to rotate around the turret very close to it or very far ? I want to add a radius variable so I can change and set the radius.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

This line: transform.RotateAround(Turret.transform.position, axis.normalized, angle); is saying rotate the cube around the turret. So if you want to rotate farther away you need to move the cube farther away. To do this first we find the current vector representing the current radius which would simply be position of the cube minus the position of the turret. This gives us the radius from the turret. Then let us say you want a radius of 40, this is the current radius normalized (the same vector but of length 1) multiplied by 40. So this will be the new radius from the turret. Should look something like this:

public float radius = 40;

void Start()
 {
        if (radius > 0)
        {
             var newPos = (transform.position -Turret.transform.position).normalized * radius;
                newPos += Turret.transform.position;
                transform.position = newPos;
        }
 }
\$\endgroup\$
3
  • \$\begingroup\$ This is working great in the Start. And if I want to do it in the Update too so I will be able to change the radius while the game is running in real time ? Just adding this to the Update won't work. \$\endgroup\$
    – Daniel Lip
    Apr 7, 2018 at 5:57
  • \$\begingroup\$ i updated code, probably what is happening when radius is zero you get a normalized vector of length zero in future iterations, which is not what we want. \$\endgroup\$
    – fYre
    Apr 7, 2018 at 6:37
  • \$\begingroup\$ Working the problem is for example if I set the radius to 1 then it's stuck or rotating above the object but then when I set the radius back to 40 it keep staying on 1 it's not moving back to the 40 radius track. That's when it's in the Update. When the code is in the Update and I change to 1 and then to 40 again while the game is running. It's changing to 1 fine but it's not changing it back to radius 40. \$\endgroup\$
    – Daniel Lip
    Apr 7, 2018 at 9:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .