Z axis glitches object in two positions but not any other axis

I have a script and only on the z axis the object flashes in 2 positions. Please help.

using UnityEngine;

public class SphereMovement : MonoBehaviour
{
    public float moveSpeed = 1f;
    public float changeDirectionInterval = 2f; // Interval to change direction
    public Vector3 minBounds;
    public Vector3 maxBounds;
    private Vector3 moveDirection;

    private void Start()
    {
        // Choose a random initial movement direction
        ChangeDirection();
    }

    private void Update()
    {
        // Move the sphere in the current direction
        transform.Translate(moveDirection * moveSpeed * Time.deltaTime);

        // Check if it's time to change direction
        changeDirectionInterval -= Time.deltaTime;
        if (changeDirectionInterval <= 0f)
        {
            // Choose a new random direction
            ChangeDirection();

            // Reset the interval
            changeDirectionInterval = Random.Range(1f, 3f); // You can adjust the range of interval here
        }

        // Ensure the sphere stays within the defined boundaries
        ClampPosition();

        // Debug logging
        Debug.Log("Position: " + transform.position);
        Debug.Log("Movement Direction: " + moveDirection);
    }

    private void ClampPosition()
    {
        // Clamp position to stay within the defined boundaries
        Vector3 clampedPosition = transform.position;
        clampedPosition.x = Mathf.Clamp(clampedPosition.x, minBounds.x, maxBounds.x);
        clampedPosition.y = Mathf.Clamp(clampedPosition.y, minBounds.y, maxBounds.y);
        clampedPosition.z = Mathf.Clamp(clampedPosition.z, minBounds.z, maxBounds.z);
        transform.position = clampedPosition;
    }

    private void ChangeDirection()
    {
        // Choose a new random direction
        moveDirection = Random.insideUnitSphere.normalized;
    }
}

Heres the debug log:

Movement Direction: (0.10, 0.09, 0.99)
UnityEngine.Debug:Log (object)
SphereMovement:Update () (at Assets/SphereMovement.cs:38)

Position: (-1.18, 2.49, 4.50)
UnityEngine.Debug:Log (object)
SphereMovement:Update () (at Assets/SphereMovement.cs:37)

Movement Direction: (0.10, 0.09, 0.99)
UnityEngine.Debug:Log (object)
SphereMovement:Update () (at Assets/SphereMovement.cs:38)

Position: (-1.18, 2.49, -4.50)
UnityEngine.Debug:Log (object)
SphereMovement:Update () (at Assets/SphereMovement.cs:37)

Movement Direction: (0.10, 0.09, 0.99)
UnityEngine.Debug:Log (object)
SphereMovement:Update () (at Assets/SphereMovement.cs:38)

Position: (-1.18, 2.49, 4.50)
UnityEngine.Debug:Log (object)
SphereMovement:Update () (at Assets/SphereMovement.cs:37)

Movement Direction: (0.10, 0.09, 0.99)
UnityEngine.Debug:Log (object)
SphereMovement:Update () (at Assets/SphereMovement.cs:38)

Position: (-1.18, 2.49, -4.50)
UnityEngine.Debug:Log (object)
SphereMovement:Update () (at Assets/SphereMovement.cs:37)

Movement Direction: (0.10, 0.09, 0.99)
UnityEngine.Debug:Log (object)
SphereMovement:Update () (at Assets/SphereMovement.cs:38)

And these are my bounds:
image

I do not understand why it is only on the z axis.

To clarify i do not want the object to flash in multiple positions that is what i want to fix. :slight_smile:

1 Answer

1

Your min and max z components are swapped.

  • minBounds.z should be -4.5
  • maxBounds.z should be 4.5

Thank you it works now, can't believe it was that simple. :grinning:

–