Advertisement
MariuszPoz

Untitled

Oct 27th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Rotation : MonoBehaviour
  6. {
  7.  
  8.     // 1. rotating a sphere around it's own axis
  9.     public float rotationSpeed = 20f; // 20 degrees per second
  10.  
  11.     // 2. for rotating around the "Sun"
  12.  
  13.     public Transform pivotPoint; // The point around which the sphere will orbit
  14.     public float rotationSunSpeed = 10f; // Degrees per second
  15.     public float startingDistance = 50f; // Initial distance from the pivot point
  16.  
  17.     void Start()
  18.     {
  19.         // Calculate the initial position based on the starting distance
  20.         Vector3 offset = transform.position - pivotPoint.position;
  21.         offset = offset.normalized * startingDistance;
  22.         transform.position = pivotPoint.position + offset;
  23.     }
  24.  
  25.     void Update()
  26.     {
  27.         // 1. Rotate the sphere by the specified speed around its up (Y) axis
  28.         transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
  29.  
  30.         // 2. for rotating around tHe Sun
  31.  
  32.         // Calculate the rotation axis using the pivot point
  33.         Vector3 pivotToSphere = transform.position - pivotPoint.position;
  34.         Vector3 rotationAxis = Vector3.up; // You can change this to a different axis if needed
  35.  
  36.         // Rotate the sphere around the pivot point
  37.         transform.RotateAround(pivotPoint.position, rotationAxis, rotationSunSpeed * Time.deltaTime);
  38.     }
  39. }
  40.  
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement