VR application issue

  Kiến thức lập trình

ia am working on a VR application in Unity where you need to rotate clock parts around a central mechanism in a circular path, maintaining a 3-meter distance.i need for help using c#language

using UnityEngine;

public class RotateClock : MonoBehaviour
{
public GameObject clock3;
public GameObject clockparts; // Now handling movement
public GameObject moveparts; // Now handling clock parts
public GameObject mechanicalparts; // Remains as mechanical parts
public GameObject player; // Reference to the player
public float rotationSpeed = 0.5f; // Slow rotation speed in degrees per second
public float moveDistance = 6f; // Distance at which the parts should start moving

private Vector3 centralPoint;
private Quaternion originalRotation;
private Vector3 originalPosition;
private float previousDistanceToPlayer;

void Start()
{
    // Calculate the central point between the three reference objects
    centralPoint = (clockparts.transform.position +
                    moveparts.transform.position +
                    mechanicalparts.transform.position) / 3f;

    // Store the original rotation and position of clock3
    originalRotation = clock3.transform.rotation;
    originalPosition = clock3.transform.position;

    // Initialize previous distance to player
    previousDistanceToPlayer = Vector3.Distance(player.transform.position, centralPoint);
}

void Update()
{
    // Check the current distance between the player and the central point
    float currentDistanceToPlayer = Vector3.Distance(player.transform.position, centralPoint);

    // Only perform rotation if the player's distance changes from within moveDistance to outside moveDistance
    if (previousDistanceToPlayer <= moveDistance && currentDistanceToPlayer > moveDistance)
    {
        // Player moved away, reset clock3 to its original position and rotation
        clock3.transform.position = originalPosition;
        clock3.transform.rotation = originalRotation;
    }
    else if (currentDistanceToPlayer <= moveDistance)
    {
        // Player is within moveDistance, rotate clock3 around the central point
        float angle = Time.time * rotationSpeed; // Use rotationSpeed for the angle calculation

        // Calculate the radius as the distance between one reference object and the central point
        float radius = Vector3.Distance(clockparts.transform.position, centralPoint);

        // Calculate the new position in a circular path
        Vector3 newPosition = centralPoint + new Vector3(Mathf.Cos(angle) * radius, 0, Mathf.Sin(angle) * radius);

        // Update the position and rotation of clock3
        clock3.transform.position = newPosition;
        clock3.transform.rotation = Quaternion.LookRotation(centralPoint - newPosition) * originalRotation;
    }

    // Update previous distance to player for the next frame
    previousDistanceToPlayer = currentDistanceToPlayer;
}

}
i neeed for clock3 is center object remiing parts rotate the clock3 on player close in clock3,after move playeer clock3 original position moviing

New contributor

Nagendran Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT