Search Unity

Character movement rotating around a central point

Discussion in 'Animation' started by stuartlangfield, Nov 7, 2017.

  1. stuartlangfield

    stuartlangfield

    Joined:
    Jul 21, 2017
    Posts:
    13
    I'm building an AR platformer test and have a specific question about constraining a characters movement to rotate in a circular motion around a central pivot point.

    Currently the level builds out in a long line along x space, and so the characters movement is restricted to X movement tied to left/right controls, plus a jump button that allows him to obviously jump, so 3D space but essentially 2D movement. So the player has to walk along and follow the characters movement in X space as they move through the level.

    However, I'd like to build a version where the level is circular, and surrounds the player so they can just rotate their body to track along with the characters movement. I've looked through a bunch of other forum posts but can't figure out how to apply them to my particular character setup.

    Some ideas or things I've already investigated:

    1. Wondering if there's a simple parenting solution where the character is parented to an empty game object, and my playerController script is adjusted to control rotation of the game object versus direction of character.

    2. Done a little research into rotateAround but I don't fully understand the concept.
    Current player controller code is pasted below if that provides further context.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     //Movement
    9.     public float speed = 10, jumpVelocity = 10;
    10.     public LayerMask playerMask;
    11.     public bool canMoveInAir = true;
    12.     public AudioSource[] AudioClips = null;
    13.     public float circumference = 4;
    14.     public float count = 0;
    15.     public Transform pivotPoint;
    16.     Vector3 previousPosition;
    17.  
    18.  
    19.     //Combat
    20.     public int health =3;
    21.     public float invincibleTimeAfterHurt = 2;
    22.  
    23.     //Teleporation
    24.     //public Transform destination01;
    25.     //public Transform destination02;
    26.     //public Transform destination03;
    27.  
    28.     //References
    29.     private gameMaster gm;
    30.  
    31.     Transform myTrans, tagGround;
    32.     Rigidbody myBody;
    33.     //GetComponent<Rigidbody> myBody;
    34.     bool isGrounded = false;
    35.     float hInput;
    36.     AnimatorController myAnim;
    37.  
    38.  
    39.  
    40.  
    41.     void Start ()
    42.     {
    43.         myBody = this.GetComponent<Rigidbody>();
    44.         myTrans = this.transform;
    45.         tagGround = GameObject.Find (this.name + "/tag_ground").transform;
    46.         myAnim = AnimatorController.instance;
    47.         gm = GameObject.FindGameObjectWithTag ("gameMaster").GetComponent<gameMaster> ();
    48.     }
    49.    
    50.     void Update ()
    51.     {
    52.     count += Time.deltaTime * speed;
    53.     var x = Mathf.Cos(count) * circumference;
    54.     var y = Mathf.Sin(count) * circumference;
    55.     var z = Mathf.Sin(count) * circumference;
    56.     Vector3 targetPosition = new Vector3(x, 0, z);
    57.     transform.position = targetPosition + pivotPoint.position;
    58.     }
    59.  
    60.  
    61.     void FixedUpdate ()
    62.     {
    63.         isGrounded = Physics.Linecast (myTrans.position, tagGround.position, playerMask);
    64.         myAnim.UpdateIsGrounded (isGrounded);
    65.  
    66.  
    67.         #if !UNITY_ANDROID && !UNITY_IPHONE || UNITY_EDITOR
    68.         Move (Input.GetAxisRaw("Horizontal"));
    69.         myAnim.UpdateSpeed(hInput);
    70.         if (Input.GetButtonDown ("Jump"))
    71.             Jump ();
    72.         //GetComponent<AudioSource>().Play();
    73.        
    74.         #endif
    75.  
    76.         Move (hInput);
    77.     }
    78.  
    79.     void Move(float horizontalInput)
    80.     {
    81.         if (!canMoveInAir && !isGrounded)
    82.             return;
    83.  
    84.         Vector3 moveVel = myBody.velocity;
    85.         moveVel.x = horizontalInput * speed;
    86.         myBody.velocity = moveVel;
    87.     }
    88.  
    89.     public void Jump()
    90.     {
    91.         if(isGrounded)
    92.         myBody.velocity += jumpVelocity * Vector3.up;
    93.         if (!AudioClips[1].isPlaying)
    94.         {
    95.             AudioClips[1].Play();
    96.         }
    97.     }
    98.  
    99.     public void StartMoving (float horizontalInput)
    100.     {
    101.         hInput = horizontalInput;
    102.         myAnim.UpdateSpeed(horizontalInput);
    103.     }
    104.  
    105.  
    106.     void Hurt()
    107.     {
    108.         health--;
    109.         if (health <= 0)
    110.             //SceneManager.LoadScene (SceneManager.LoadedScene):
    111.             //Application.LoadLevel (Application.loadedLevel);
    112.             SceneManager.LoadScene("UnityARKitMario13", LoadSceneMode.Single);
    113.         else
    114.             myAnim.TriggerHurt (invincibleTimeAfterHurt);
    115.     }
    116.  
    117.     void OnCollisionEnter (Collision collision)
    118.     {
    119.         Enemy enemy = collision.collider.GetComponent<Enemy> ();
    120.         if (enemy != null) {
    121.             if (!AudioClips[0].isPlaying)
    122.             {
    123.                 AudioClips[0].Play();
    124.             }
    125.             foreach (ContactPoint point in collision.contacts) {
    126.                 Debug.Log (point.normal);
    127.                 Debug.DrawLine (point.point, point.point + point.normal, Color.red, 10);
    128.                 if (point.normal.y >= 1f) {
    129.                     Vector3 velocity = myBody.velocity;
    130.                     velocity.y = jumpVelocity;
    131.                     myBody.velocity = velocity;
    132.                     enemy.Hurt ();
    133.                 } else {
    134.                     Hurt ();
    135.                 }
    136.             }
    137.         }
    138.     }
    139.  
    140.     void OnTriggerEnter (Collider collision)
    141.     {
    142.         if (collision.CompareTag ("Coin")) {
    143.             Destroy (collision.gameObject);
    144.             gm.points += 1;
    145.             if (!AudioClips [2].isPlaying) {
    146.                 AudioClips [2].Play ();
    147.             }
    148.         }
    149.     }
    150. }