Search Unity

Trying to make a figure 8

Discussion in 'Scripting' started by Jeriko2k3, Apr 8, 2015.

  1. Jeriko2k3

    Jeriko2k3

    Joined:
    Mar 18, 2015
    Posts:
    28
    I am working with rotating objects around each other. Right now I have a serious of cubes that are rotating around each other, and it is working well.

    If you look at my picture, there are two large cubes that do not move or rotate. There are two smaller cubes that I have orbiting around them like small moons (both are childs to their releative positions). I am trying to make these small moons change parents yet continue to rotate around the new parent in and they would continue to swap until infinity. Right now all I seem to be doing is causing some sort of collision between the two though nothing touches and the two big squares act as if they were struck and begin to drift away from each other. Any help would be....helpful.

    Thanks.
    Code (CSharp):
    1. sing UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Orbitrotate : MonoBehaviour {
    5.    
    6.     void Update()
    7.        
    8.     {
    9.        
    10.         transform.Rotate(new Vector3 (0,0 ,50) * Time.deltaTime );
    11.        
    12.     }
    13.  
    14.     void OnTriggerEnter (Collider other)
    15.  
    16.     {
    17.  
    18.         if (other.gameObject.tag  == "Test")
    19.  
    20.         {
    21.             other.transform.parent=transform;
    22.  
    23.         }
    24.  
    25.     }
    26. }
    upload_2015-4-7_19-43-55.png



    upload_2015-4-7_19-50-26.png
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    I see, so you want to swap the parents and rotate around the new transform. I made a script that does exactly what you want its based off of spherical coordinates, using rectangular coordinates is a pain.

    I'm sure you can tailer it to your needs. Hope this helps.

    Orbit.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Orbit : MonoBehaviour {
    5.     [SerializeField] Transform target; // Target assinged via inspector.
    6.     [SerializeField] bool lookAtTarget = true;
    7.     [SerializeField] float targetFindDelay = 0.1f; // Delay before searching for the target.
    8.     [SerializeField] float minDistance = 1.0f; // Minimum zoom distance.
    9.     [SerializeField] float maxDistance = 5.0f; // Maximum zoom distance.
    10.     [SerializeField] float rotateSpeed = 20.0f; // The speed the look rotation.
    11.     [SerializeField] float movementSpeed = 20.0f; // The speed of positioning via the spherical bounds.
    12.     [SerializeField] float thetaSteps = 4.0f; // The amount of degrees to move via each request.
    13.     [SerializeField] float phiSteps = 4.0f; // the amount of degree to move via each request.
    14.     [SerializeField] float upperBound = 0.1f; // Upper bound of the Phi angle from the Y axis.
    15.     [SerializeField] float lowerBound = 90.0f; // Lower bound of the Phi angle form the Y Axis.
    16.     [SerializeField] float zoomSpeed = 15.0f; // The speed at which the radius will change.
    17.    
    18.     float r = 1.0f; // Distance from the target.
    19.     float thetaAngle = 0.0f; // Theta angle for horizontal rotations.
    20.     float phiAngle = 0.0f; // Phi angle for vertical rotations
    21.     float _maxDistance = 0.0f;
    22.    
    23.     void Start() {
    24.         this.phiAngle = (this.lowerBound - this.upperBound) / 2.0f; // Set the Phi angle between the lower and upper bound
    25.         this.r = (this.maxDistance - this.minDistance) / 2.0f; // Set the starting radius between the min and max distance.
    26.         this._maxDistance = this.maxDistance;
    27.        
    28.         // Check if the target is null.
    29.         if(this.target == null) {
    30.             UnityEngine.Debug.LogWarning(typeof(Orbit).ToString() + " - Target is Null removing the component");
    31.             Object.Destroy(this);
    32.         }
    33.     }
    34.    
    35.     /// <summary>
    36.     /// Gets the Z position in rectanglular coordinates from spherical coordinates
    37.     /// </summary>
    38.     /// <value>The Z position.</value>
    39.     float ZPosition {
    40.         get {
    41.             return this.r * Mathf.Sin(this.thetaAngle * (Mathf.PI / 180.0f)) * Mathf.Sin(this.phiAngle * (Mathf.PI / 180.0f)) + this.target.position.z;
    42.         }
    43.     }
    44.    
    45.     /// <summary>
    46.     /// Gets the Y position in rectanglular coordinates from spherical coordinates
    47.     /// </summary>
    48.     /// <value>The Y position.</value>
    49.     float YPosition {
    50.         get {
    51.             return this.r * Mathf.Cos(this.phiAngle * (Mathf.PI / 180.0f)) + this.target.position.y;
    52.         }
    53.     }
    54.    
    55.     /// <summary>
    56.     /// Gets the X position in rectanglular coordinates from spherical coordinates
    57.     /// </summary>
    58.     /// <value>The X position.</value>
    59.     float XPosition {
    60.         get {
    61.             return this.r * Mathf.Cos(this.thetaAngle * (Mathf.PI / 180.0f)) * Mathf.Sin(this.phiAngle * (Mathf.PI / 180.0f)) + this.target.position.x;
    62.         }
    63.     }
    64.    
    65.     /// <summary>
    66.     /// Gets the behind positional angle.
    67.     /// </summary>
    68.     /// <value>The behind position.</value>
    69.     float BehindPosition {
    70.         get {
    71.             return (360.0f - this.target.eulerAngles.y) - 90.0f;
    72.         }
    73.     }
    74.    
    75.     void Update() {
    76.         if(this.target == null) return;
    77.        
    78.         this.transform.position = Vector3.MoveTowards(this.transform.position, this.TargetPosition, this.movementSpeed * Time.deltaTime);
    79.  
    80.         if(this.lookAtTarget == true)
    81.             this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(this.target.position - this.transform.position), this.rotateSpeed * Time.deltaTime);
    82.     }
    83.    
    84.     void OnDrawGizmosSelected() {
    85.         if(this.target != null) {
    86.             Gizmos.color = Color.red;
    87.             Gizmos.DrawWireSphere(this.target.position, this.r);
    88.             Gizmos.color = Color.white;
    89.         }
    90.     }
    91.  
    92.     /// <summary>
    93.     /// Sets the phi angle.
    94.     /// </summary>
    95.     /// <param name="angle">Angle.</param>
    96.     public void SetPhiAngle(float angle) {
    97.         angle = Mathf.Clamp(angle, this.upperBound, this.lowerBound);
    98.  
    99.         this.phiAngle = angle;
    100.     }
    101.  
    102.     /// <summary>
    103.     /// Sets the radius.
    104.     /// </summary>
    105.     /// <param name="radius">Radius.</param>
    106.     public void SetRadius(float radius) {
    107.         radius = Mathf.Clamp(radius, this.minDistance, this.maxDistance);
    108.  
    109.         this.r = radius;
    110.     }
    111.  
    112.     /// <summary>
    113.     /// Sets the orbit target.
    114.     /// </summary>
    115.     /// <param name="target">Target.</param>
    116.     public void SetOrbitTarget(Transform target) {
    117.         this.target = target;
    118.     }
    119.    
    120.     /// <summary>
    121.     /// Sets this gameObject behind the forward facing of the target.
    122.     /// </summary>
    123.     public void SetBehindTarget() {
    124.         this.thetaAngle = this.BehindPosition;
    125.     }
    126.  
    127.     /// <summary>
    128.     /// Clamps the radius.
    129.     /// </summary>
    130.     /// <param name="maxRadius">Max radius.</param>
    131.     public void ClampRadius(float maxRadius = -1.0f) {
    132.         _maxDistance = maxRadius;
    133.        
    134.         this.r = Mathf.Clamp(this.r, this.minDistance, this._maxDistance);
    135.     }
    136.    
    137.     /// <summary>
    138.     /// Moves the gameObject upward bounded by the sphereical coordinate system.
    139.     /// </summary>
    140.     public void MoveUp() {
    141.         this.phiAngle -= this.phiSteps;
    142.        
    143.         this.phiAngle = Mathf.Clamp(this.phiAngle, this.upperBound, this.lowerBound);
    144.         this.phiAngle = Mathf.Clamp(this.phiAngle, 0.1f, 179.9f);
    145.     }
    146.    
    147.     /// <summary>
    148.     /// Moves the gameObject downward bounded by the sphereical coordinate system.
    149.     /// </summary>
    150.     public void MoveDown() {
    151.         this.phiAngle += this.phiSteps;
    152.        
    153.         this.phiAngle = Mathf.Clamp(this.phiAngle, this.upperBound, this.lowerBound);
    154.         this.phiAngle = Mathf.Clamp(this.phiAngle, 0.1f, 179.9f);
    155.     }
    156.    
    157.     /// <summary>
    158.     /// Move the gameObject counter clockwise.
    159.     /// Reference Vector <1, 0, 1>
    160.     /// </summary>
    161.     public void CounterClockWise() {
    162.         this.thetaAngle += this.thetaSteps;
    163.        
    164.         if(this.thetaAngle >= 360.0f) {
    165.             this.thetaAngle = 0.0f;
    166.         }
    167.     }
    168.    
    169.     /// <summary>
    170.     /// Move the gameObject clockwise.
    171.     /// Reference Vector <1, 0, 1>
    172.     /// </summary>
    173.     public void ClockWise() {
    174.         this.thetaAngle -= this.thetaSteps;
    175.        
    176.         if(this.thetaAngle < 0.0f) {
    177.             this.thetaAngle = 360.0f;
    178.         }
    179.     }
    180.    
    181.     /// <summary>
    182.     /// Gets the target position.
    183.     /// </summary>
    184.     /// <value>The target position.</value>
    185.     public Vector3 TargetPosition {
    186.         get {
    187.             return new Vector3(this.XPosition, this.YPosition, this.ZPosition);
    188.         }
    189.     }
    190. }
    191.  
    Orbiter.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Orbiter : MonoBehaviour {
    5.     [SerializeField] Orbit orbit;
    6.     [SerializeField] OrbitType orbitType = OrbitType.ClockWise;
    7.     [SerializeField] float phiAngle = 90.0f;
    8.     [SerializeField] float distance = 10.0f;
    9.  
    10.     void Start() {
    11.         if(this.orbit == null) {
    12.             UnityEngine.Debug.LogWarning(typeof(Orbiter).ToString() + " - Orbit has not been set!");
    13.             Object.Destroy(this);
    14.         }
    15.     }
    16.  
    17.     void Update() {
    18.         switch(this.orbitType) {
    19.         case OrbitType.ClockWise:
    20.         {
    21.             this.orbit.ClockWise();
    22.         }
    23.             break;
    24.         case OrbitType.CounterClockWise:
    25.         {
    26.             this.orbit.CounterClockWise();
    27.         }
    28.             break;
    29.         }
    30.  
    31.         this.orbit.SetPhiAngle(this.phiAngle);
    32.         this.orbit.SetRadius(this.distance);
    33.     }
    34.  
    35.     enum OrbitType {
    36.         ClockWise,
    37.         CounterClockWise
    38.     }
    39. }
    40.  
     
  3. Jeriko2k3

    Jeriko2k3

    Joined:
    Mar 18, 2015
    Posts:
    28
    thanks, think I got this figured out now!