Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Third Person Top-Down Camera with Faux Gravity

Discussion in 'Scripting' started by Goopher, Jan 18, 2017.

  1. Goopher

    Goopher

    Joined:
    Feb 1, 2016
    Posts:
    6
    Dear community!

    I'm struggling while making a faux gravity based game (planetary gravity). I've got my character controller set up and while testing I have used my main camera as a child object of my player object. This, however, creates stuttering and jittering while moving. I want to move my camera now through scripting so I can maybe try lerp and not have this jitter anymore. I have the following code in my camera script which correctly follows the player but when I want to rotate with the player as well the camera moves all weirdly.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraController : MonoBehaviour {
    5.  
    6.     public GameObject player;       //Public variable to store a reference to the player game object
    7.  
    8.  
    9.     private Vector3 offset;         //Private variable to store the offset distance between the player and camera
    10.  
    11.     // Use this for initialization
    12.     void Start ()
    13.     {
    14.         //Calculate and store the offset value by getting the distance between the player's position and camera's position.
    15.         offset = transform.position - player.transform.position;
    16.     }
    17.  
    18.     // LateUpdate is called after Update each frame
    19.     void LateUpdate ()
    20.     {
    21.         // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
    22.         transform.position = player.transform.position + offset;
    23.  
    24.  
    25.     }
    26. }
    Here you can find a video of my issue:



    Can anyone point me in the right direction?

    Kind regards
     
  2. Goopher

    Goopher

    Joined:
    Feb 1, 2016
    Posts:
    6
    I solved it myself by using the following (messy) code (this is my entire playercontroller script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     [SerializeField] float walkSpeed;
    8.     float runSpeed;
    9.     bool running;
    10.     float moveHorizontal;
    11.     Vector3 rotation;
    12.     float moveVertical;
    13.     Vector3 movementDirection;
    14.     public Transform model;
    15.     Animator anim;
    16.     bool shooting;
    17.     float cooldown;
    18.     public GameObject bullet;
    19.     public GameObject planet;
    20.  
    21.  
    22.     Rigidbody rb;
    23.  
    24.     // Use this for initialization
    25.     void Start () {
    26.         anim = GetComponent<Animator> ();
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update () {
    31.         // state variables;
    32.         running = false;
    33.         shooting = false;
    34.         runSpeed = walkSpeed;
    35.         cooldown -= Time.deltaTime;
    36.  
    37.         // calculate movement direction
    38.         moveHorizontal = Mathf.Clamp(Input.GetAxis ("Horizontal"), -0.99f, 0.99f);
    39.         moveVertical = Input.GetAxis ("Vertical");
    40.         movementDirection = new Vector3 (moveHorizontal, 0f, moveVertical).normalized;  
    41.  
    42.         // calculate rotation
    43.         rotation.y = ((Mathf.Atan2 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical")) * (180 / Mathf.PI)));
    44.         RotateIfNewDirection (rotation);
    45.  
    46.  
    47.         // Inputs
    48.         if (Input.GetButton ("Fire1") && cooldown < 0) {
    49.             Shoot ();
    50.         }
    51.  
    52.            
    53.         if (Input.GetKey (KeyCode.LeftShift)) {
    54.             running = true;
    55.             runSpeed = 8;
    56.  
    57.         }
    58.  
    59.         print ((movementDirection * walkSpeed).magnitude);
    60.    
    61.         if (!shooting) {
    62.                 transform.Translate (movementDirection * runSpeed * Time.fixedDeltaTime, Space.Self);
    63. // this is the key line of code for this to work
    64.                 Camera.main.transform.RotateAround (planet.transform.position, transform.TransformDirection(new Vector3(moveVertical, 0, -moveHorizontal)), RotationAnglePerSecond() * Time.fixedDeltaTime);
    65.         }
    66.  
    67.         // animation
    68.         Animate ();
    69.     }
    70.  
    71.     void FixedUpdate () {
    72.        
    73.     }
    74.  
    75.     private void RotateIfNewDirection (Vector3 input) {
    76.        
    77.         if ((movementDirection * walkSpeed * Time.fixedDeltaTime).magnitude == 0f) {
    78.             return;
    79.         }
    80.  
    81.         model.localEulerAngles = rotation;
    82.     }
    83.  
    84.     private float RotationAnglePerSecond () {
    85.         float diameter = planet.transform.localScale.y/2;
    86.         float circumference = (2 * Mathf.PI) * diameter;
    87.         float secondsToCirclePlanet = circumference / (movementDirection * runSpeed).magnitude;
    88.         float degreesPerSecond = 360 / secondsToCirclePlanet;
    89.         print (circumference);
    90.         return degreesPerSecond;
    91.  
    92.     }
    93.  
    94.  
    95.  
    96. }
    97.  
     
  3. Empiregaming

    Empiregaming

    Joined:
    Jun 25, 2017
    Posts:
    1
    can you show what camera script you used?....even i am facing same problem ....i am newbie and cannot understand your player controller script
     
  4. Igor_Vasiak

    Igor_Vasiak

    Joined:
    Dec 13, 2016
    Posts:
    42
    The camera was glitching because the player is always being pulled down to the center of the sphere, and then being thrown away by the collider (it has only a range of milimiters, that's why it's invisible). Because the camera is a child of the player, the camera goes with the player. The best solution for this problem is, really, the follow script. Congratulations for solving this problem by yourself. I'm just posting this here for who want's to know. If I made a mistake, please correct me.
     
  5. zZneR

    zZneR

    Joined:
    May 20, 2019
    Posts:
    1
    Hey sorry. I am a newbie and my question is a little of topic but I am trying to achieve the same player movement as you have going on here using a joystick. I have no problem moving the player object around the sphere but I have been stuck trying to figure out how to make it face the direction its moving. How were you able to do it? I've been trying to make sense out of your player script but I'm not sure if its the full code you've implemented or not. Thank you
     
unityunity