0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateAroundTarget : MonoBehaviour
{
    public Transform target;
    public float speed;
    public Vector3 axis;
    public DrawCircle dc;

    private float lastRadius;
    private bool move = false;

    private void Start()
    {
        lastRadius = dc.xRadius;

        move = true;
    }

    private void Update()
    {
        Vector3 radiusPosition = new Vector3(target.position.x, target.position.y, dc.xRadius);

        if (transform.position == radiusPosition)
        {
            transform.RotateAround(target.position, axis, speed * Time.deltaTime);
        }

        if (lastRadius != dc.xRadius)
        {
            move = true;
            lastRadius = dc.xRadius;
        }

        if(move)
        {
            if (transform.position != radiusPosition)
            {
                float step = 1 * Time.deltaTime;
                transform.position = Vector3.MoveTowards(transform.position,
                    radiusPosition, step);
            }
        }
    }
}

The transform is moving towards the radius position and then should start rotating on the radius.

The problem is that after the transform is reaching the radius position and for example the transform position and the radius position is the same and true :

Same position

In this case the position of the transform and the radiusPosition is "(0.0, 0.0, 2.0)" and the condition is true but still it's getting inside and changing the transform position.

So it's not moving between the lines :

transform.RotateAround(target.position, axis, speed * Time.deltaTime);

And

float step = 1 * Time.deltaTime;
                transform.position = Vector3.MoveTowards(transform.position,
                    radiusPosition, step);

I can't figure out why.

What i'm trying to archive is to make the transform to move on the start towards the radius and then to start rotating around on the radius. but for some reason the IF conditions are not working as expected.

Transform is in radius position

The transform is rotating around it self and shaking like it's trying to move/rotatearound but never rotate around.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The problem is you are using if (transform.position == radiusPosition) and it rarely becomes true because the two vectors are rarely the same. (even if they are visually the same Vector, your programs detects a very small difference like 0.000007)

What you should do is using this code: if(Vector3.Distance(transform.position, radiusPosition) <= 0.2f) And using it, transform.RotateAround(target.position, axis, speed * Time.deltaTime); should be called just as you've expected

\$\endgroup\$

You must log in to answer this question.

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