Search Unity

Player Controller in a circular motion

Discussion in 'Scripting' started by Kiesco91, Jan 10, 2019.

  1. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    hello,

    I'm trying my hand at my first 3D game but am struggling with the controller. i have tried a few different ideas but none that do what I'm after. i have a Cube game object in the middle of the screen and the player object facing the cube from a distance. i want to make the player be able to move 360 degrees around the cube while the camera is locked looking at the cube. i want to be able to move the player myself either left or right in a circular motion using the A and D keys rather than it constantly orbiting.

    i have tried a few different scripts but nothing is giving me this simple movement structure.

    thanks,
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Do you want to rotate the play object around the cube? You could make a pivot point the child of the cube, and then the player a child of the pivot.
    -Cube
    --Pivot -> Attach Rotation Controller
    ---Player

    Then a script like this would be attached to the Pivot:
    Code (csharp):
    1. public class RotationController : MonoBehaviour
    2. {
    3.    public float rotationSpeed = 30.0f; // Speed in degrees per second
    4.  
    5.    private void Update()
    6.    {
    7.       if(Input.GetKey(KeyCode.A))
    8.          transform.Rotate(new Vector3(0,rotationSpeed * Time.deltaTime,0));
    9.       if(Input.GetKey(KeyCode.D))
    10.          transform.Rotate(new Vector3(0,-rotationSpeed * Time.deltaTime,0));
    11.    }
    12. }
     
  3. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    Hello,

    That sounds like it would work!

    Will try tomorrow and report back.

    Thanks!