Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rotating around a player while moving up the y-axis.

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

  1. Jeriko2k3

    Jeriko2k3

    Joined:
    Mar 18, 2015
    Posts:
    28
    What I am wanting to do:

    Player controller hits an object. Object then becomes a child of the collider rotating around the Player Controller while going up the Y-Axis. Object then gets destroyed upon hitting a destroy collider.


    What is currently happening:

    Player controller collides with object, object rotates around the base of Player controller. If it collides with anything else locks the game down. Behind the scenes, I have an orbit (empty gameobject) inside the player controller program used to rotate the object, and collider attached to that used to pick up object (changes parents). However this collider is also rotating around it's parent (I understand why it is doing that) I am needing it frozen and cannot figure out how to lock it in place (I am guessing I would need it to not be a child of the orbiter gameobject, but still turn the object collided into a child of the orbiter gameobject). I also have created a platform program my thought was to have it be an empty object as well, use it to guide the object I collided into up the y-axis and the orbit program would slingshot it around the player controller. (Still with me?)
    All I managed to do was to have the platform program rotate around the orbiter gameobject and float off into space (which is somewhat what I want the object I collide into to do!) I'm pulling out what little hair I have.

    So if this makes sense to any of you, please point me in the right direction. Anything is helpful. I will post my scripts after this post.

    Thanks,

    Jeri


    And thanks to those that have helped with my other issues
     
  2. Jeriko2k3

    Jeriko2k3

    Joined:
    Mar 18, 2015
    Posts:
    28
    this is my parent swap and platform concept


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Parentswap : MonoBehaviour
    5.  
    6. {
    7.        
    8.         [SerializeField]
    9.         Transform platforms;
    10.        
    11.         [SerializeField]
    12.         Transform startTransform;
    13.        
    14.         [SerializeField]
    15.         Transform endTransform;
    16.        
    17.         [SerializeField]
    18.         float platformSpeed;
    19.        
    20.         Vector3 direction;
    21.         Transform destination;
    22.  
    23.  
    24.     void OnTriggerEnter(Collider other)
    25.        
    26.     {
    27.    
    28.         if (other.gameObject.tag == "gameObject")
    29.        
    30.         {
    31.             other.transform.parent = transform;
    32.        
    33.         }
    34.    
    35.     }
    36.  
    37.         void Start ()
    38.            
    39.         {
    40.            
    41.             SetDestination(startTransform);
    42.            
    43.         }
    44.        
    45.         void FixedUpdate()
    46.            
    47.         {
    48.            
    49.             platforms.GetComponent<Rigidbody>().MovePosition(platforms.position + direction * platformSpeed *Time.fixedDeltaTime);
    50.            
    51.             if (Vector3.Distance (platforms.position, destination.position) < platformSpeed * Time.fixedDeltaTime);
    52.            
    53.             {
    54.                
    55.                 SetDestination(destination == startTransform ? endTransform : startTransform);
    56.                
    57.             }
    58.            
    59.         }
    60.        
    61.         void OnDrawGizmos()
    62.            
    63.         {
    64.             Gizmos.color = Color.green;
    65.             Gizmos.DrawWireCube(startTransform.position, platforms.localScale);
    66.            
    67.             Gizmos.color = Color.red;
    68.             Gizmos.DrawWireCube(endTransform.position, platforms.localScale);
    69.            
    70.         }
    71.        
    72.         void SetDestination(Transform dest)
    73.            
    74.         {
    75.            
    76.             destination = dest;
    77.             direction = (destination.position = platforms.position).normalized;
    78.            
    79.         }
    80.        
    81. }
    82.  
    83.     //void Update()
    84.  
    85.     //{
    86.  
    87.     //    foreach (Transform child in transform)
    88.        
    89.         //{
    90.        
    91.         //    child.position += Vector3.up * 10.0F;
    92.  
    93.         //    transform.Translate(0, Time.deltaTime * 1, 0);
    94.  
    95.         //    float translation = Time.deltaTime * 10;
    96.         //    transform.Translate(0, 0, translation);
    97.  
    98.        
    99.         //}
    100.  
    101.     //void MoveVertical()
    102.  
    103.     //{
    104.  
    105.     //    transform.position.y = Mathf.Sin (Time.deltaTime * 10);
    106.     //
    107.     //}
    108.  
    109.  
     
  3. Jeriko2k3

    Jeriko2k3

    Joined:
    Mar 18, 2015
    Posts:
    28
    orbit program.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Orbit : MonoBehaviour
    5.    
    6.    
    7. {
    8.    
    9.    
    10.     [SerializeField] Transform target;
    11.    
    12.    
    13.     [SerializeField] bool lookAtTarget = true;
    14.    
    15.    
    16.     [SerializeField] float targetFindDelay = 0.1f;
    17.    
    18.    
    19.     [SerializeField] float minDistance = 1.0f;
    20.    
    21.    
    22.     [SerializeField] float maxDistance = 5.0f;
    23.    
    24.    
    25.     [SerializeField] float rotateSpeed = 20.0f;
    26.    
    27.    
    28.     [SerializeField] float movementSpeed = 20.0f;
    29.    
    30.    
    31.     [SerializeField] float thetaSteps = 4.0f;
    32.    
    33.    
    34.     [SerializeField] float phiSteps = 4.0f;
    35.    
    36.    
    37.     [SerializeField] float upperBound = 0.1f;
    38.    
    39.    
    40.     [SerializeField] float lowerBound = 90.0f;
    41.    
    42.    
    43.     [SerializeField] float zoomSpeed = 15.0f;
    44.    
    45.    
    46.    
    47.     float r = 1.0f;
    48.    
    49.    
    50.     float thetaAngle = 0.0f;
    51.    
    52.    
    53.     float phiAngle = 0.0f;
    54.    
    55.    
    56.     float _maxDistance = 0.0f;
    57.    
    58.    
    59.    
    60.    
    61.    
    62.     void Start()
    63.        
    64.     {
    65.        
    66.        
    67.         this.phiAngle = (this.lowerBound - this.upperBound) / 2.0f;
    68.        
    69.        
    70.         this.r = (this.maxDistance - this.minDistance) / 2.0f;
    71.        
    72.         this._maxDistance = this.maxDistance;
    73.        
    74.        
    75.         // Check if the target is null.
    76.        
    77.        
    78.         if(this.target == null)
    79.            
    80.         {
    81.            
    82.            
    83.             UnityEngine.Debug.LogWarning(typeof(Orbit).ToString() + " - Target is Null removing the component");
    84.            
    85.            
    86.             Object.Destroy(this);
    87.            
    88.            
    89.         }
    90.        
    91.        
    92.     }
    93.    
    94.    
    95.    
    96.    
    97.    
    98.     float ZPosition
    99.        
    100.     {
    101.        
    102.        
    103.         get
    104.            
    105.         {
    106.            
    107.            
    108.             return this.r * Mathf.Sin(this.thetaAngle * (Mathf.PI / 180.0f)) * Mathf.Sin(this.phiAngle * (Mathf.PI / 180.0f)) + this.target.position.z;
    109.            
    110.            
    111.         }
    112.        
    113.        
    114.     }
    115.    
    116.    
    117.    
    118.    
    119.    
    120.    
    121.     float YPosition
    122.        
    123.     {
    124.        
    125.        
    126.         get
    127.            
    128.         {
    129.            
    130.            
    131.             return this.r * Mathf.Cos(this.phiAngle * (Mathf.PI / 180.0f)) + this.target.position.y;
    132.            
    133.            
    134.         }
    135.        
    136.        
    137.     }
    138.    
    139.    
    140.    
    141.    
    142.    
    143.     float XPosition
    144.        
    145.     {
    146.        
    147.        
    148.         get
    149.            
    150.         {
    151.            
    152.            
    153.             return this.r * Mathf.Cos(this.thetaAngle * (Mathf.PI / 180.0f)) * Mathf.Sin(this.phiAngle * (Mathf.PI / 180.0f)) + this.target.position.x;
    154.            
    155.            
    156.         }
    157.        
    158.        
    159.     }
    160.    
    161.    
    162.    
    163.    
    164.    
    165.     float BehindPosition
    166.        
    167.     {
    168.        
    169.        
    170.         get
    171.            
    172.         {
    173.            
    174.            
    175.             return (360.0f - this.target.eulerAngles.y) - 90.0f;
    176.            
    177.            
    178.         }
    179.        
    180.     }
    181.    
    182.    
    183.     void Update()
    184.        
    185.     {
    186.        
    187.        
    188.         if(this.target == null) return;
    189.        
    190.        
    191.         this.transform.position = Vector3.MoveTowards(this.transform.position, this.TargetPosition, this.movementSpeed * Time.deltaTime);
    192.        
    193.        
    194.         if(this.lookAtTarget == true)
    195.            
    196.            
    197.             this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(this.target.position - this.transform.position), this.rotateSpeed * Time.deltaTime);
    198.        
    199.        
    200.     }
    201.    
    202.    
    203.    
    204.    
    205.    
    206.     void OnDrawGizmosSelected()
    207.        
    208.     {
    209.        
    210.        
    211.         if(this.target != null)
    212.            
    213.         {
    214.            
    215.             Gizmos.color = Color.red;
    216.            
    217.            
    218.             Gizmos.DrawWireSphere(this.target.position, this.r);
    219.            
    220.            
    221.             Gizmos.color = Color.white;
    222.            
    223.            
    224.         }
    225.        
    226.        
    227.     }
    228.    
    229.    
    230.    
    231.    
    232.     public void SetPhiAngle(float angle)
    233.        
    234.     {
    235.        
    236.         angle = Mathf.Clamp(angle, this.upperBound, this.lowerBound);
    237.        
    238.         this.phiAngle = angle;
    239.        
    240.        
    241.     }
    242.    
    243.    
    244.    
    245.    
    246.     public void SetRadius(float radius)
    247.        
    248.     {
    249.        
    250.         radius = Mathf.Clamp(radius, this.minDistance, this.maxDistance);
    251.        
    252.         this.r = radius;
    253.        
    254.     }
    255.    
    256.    
    257.    
    258.     public void SetOrbitTarget(Transform target)
    259.        
    260.     {
    261.        
    262.         this.target = target;
    263.        
    264.     }
    265.    
    266.  
    267.    
    268.     public void SetBehindTarget()
    269.        
    270.     {
    271.        
    272.         this.thetaAngle = this.BehindPosition;
    273.        
    274.        
    275.     }
    276.    
    277.  
    278.    
    279.     public void ClampRadius(float maxRadius = -1.0f)
    280.        
    281.     {
    282.        
    283.         _maxDistance = maxRadius;
    284.        
    285.         this.r = Mathf.Clamp(this.r, this.minDistance, this._maxDistance);
    286.        
    287.     }
    288.    
    289.  
    290.    
    291.     public void MoveUp()
    292.        
    293.     {
    294.        
    295.         this.phiAngle -= this.phiSteps;
    296.        
    297.         this.phiAngle = Mathf.Clamp(this.phiAngle, this.upperBound, this.lowerBound);
    298.        
    299.         this.phiAngle = Mathf.Clamp(this.phiAngle, 0.1f, 179.9f);
    300.        
    301.     }
    302.    
    303.    
    304.    
    305.     public void MoveDown()
    306.        
    307.     {
    308.        
    309.         this.phiAngle += this.phiSteps;
    310.        
    311.         this.phiAngle = Mathf.Clamp(this.phiAngle, this.upperBound, this.lowerBound);
    312.        
    313.         this.phiAngle = Mathf.Clamp(this.phiAngle, 0.1f, 179.9f);
    314.        
    315.     }
    316.    
    317.    
    318.    
    319.     public void CounterClockWise()
    320.        
    321.     {
    322.        
    323.         this.thetaAngle += this.thetaSteps;
    324.        
    325.         if(this.thetaAngle >= 360.0f)
    326.            
    327.         {
    328.            
    329.             this.thetaAngle = 0.0f;
    330.            
    331.         }
    332.        
    333.     }
    334.    
    335.    
    336.    
    337.     public void ClockWise()
    338.        
    339.     {
    340.        
    341.         this.thetaAngle -= this.thetaSteps;
    342.        
    343.         if(this.thetaAngle < 0.0f)
    344.            
    345.         {
    346.            
    347.             this.thetaAngle = 360.0f;
    348.            
    349.         }
    350.        
    351.     }
    352.    
    353.    
    354.    
    355.     public Vector3 TargetPosition
    356.        
    357.     {
    358.        
    359.         get
    360.            
    361.         {
    362.            
    363.             return new Vector3(this.XPosition, this.YPosition, this.ZPosition);
    364.            
    365.         }
    366.        
    367.     }
    368.    
    369. }
     
  4. Jeriko2k3

    Jeriko2k3

    Joined:
    Mar 18, 2015
    Posts:
    28
    Orbiter program.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Orbiter : MonoBehaviour
    5.    
    6. {
    7.    
    8.     [SerializeField] Orbit orbit;
    9.    
    10.     [SerializeField] OrbitType orbitType = OrbitType.ClockWise;
    11.    
    12.     [SerializeField] float phiAngle = 90.0f;
    13.    
    14.     [SerializeField] float distance = 10.0f;
    15.    
    16.     void Start()
    17.        
    18.     {
    19.        
    20.         if(this.orbit == null)
    21.            
    22.         {
    23.            
    24.             UnityEngine.Debug.LogWarning(typeof(Orbiter).ToString() + " - Orbit has not been set!");
    25.            
    26.             Object.Destroy(this);
    27.            
    28.         }
    29.        
    30.     }
    31.    
    32.     void Update()
    33.        
    34.     {
    35.        
    36.         switch(this.orbitType)
    37.            
    38.         {
    39.            
    40.         case OrbitType.ClockWise:
    41.            
    42.         {
    43.            
    44.             this.orbit.ClockWise();
    45.            
    46.         }
    47.            
    48.             break;
    49.            
    50.            
    51.         case OrbitType.CounterClockWise:
    52.            
    53.            
    54.         {
    55.            
    56.             this.orbit.CounterClockWise();
    57.            
    58.         }
    59.            
    60.             break;
    61.            
    62.         }
    63.        
    64.         this.orbit.SetPhiAngle(this.phiAngle);
    65.        
    66.         this.orbit.SetRadius(this.distance);
    67.        
    68.     }
    69.    
    70.     enum OrbitType
    71.        
    72.     {
    73.        
    74.         ClockWise,
    75.        
    76.         CounterClockWise
    77.        
    78.     }
    79.    
    80. }
    81.