Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Camera follow and orbiting

Discussion in 'Scripting' started by ReLost, Nov 8, 2015.

  1. ReLost

    ReLost

    Joined:
    Nov 8, 2015
    Posts:
    5
    Hi,
    I want to use a RollerBall prefab, but I want to change its control
    I want to make the ball move only forward and backward, so I changed the BallUserControl script to make it work. "A" and "D" used to rotate camera around the ball.
    I have implemented a CameraFollow by this code:
    Code (CSharp):
    1. public GameObject player;
    2. public Vector3 offset;
    3.  
    4. void Start()
    5.     {
    6.         offset = transform.position - player.transform.position;
    7.     }
    8.  
    9. void LateUpdate()
    10.     {
    11.         transform.position = player.transform.position + offset;
    12.         transform.LookAt(player.transform);
    13.     }
    Next, I implement a Camera orbiting around the ball, by this code:
    Code (CSharp):
    1. if (Input.GetKey(KeyCode.D))
    2.         {
    3.             transform.Translate(Vector3.right * rotateSpeed);
    4.             transform.LookAt(player.transform);
    5.         }
    6.      
    7. if (Input.GetKey(KeyCode.A))
    8.         {          
    9.             transform.Translate(Vector3.left * rotateSpeed);
    10.             transform.LookAt(player.transform);
    11.         }
    Both codes work, but only separately, together they block each other
    Camera Follow works fine, but orbiting doesn't
    How can I fix it?

    Here's whole code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FollowTrackingCamera : MonoBehaviour
    5. {
    6.  
    7.     public GameObject player;
    8.     public Vector3 offset;
    9.     public float rotateSpeed;
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         offset = transform.position - player.transform.position;
    15.     }
    16.  
    17.     void CameraFollow()
    18.     {
    19.         transform.position = player.transform.position + offset;
    20.         transform.LookAt(player.transform);
    21.     }
    22.  
    23.     void LateUpdate()
    24.     {                
    25.  
    26.         CameraFollow();
    27.         if (Input.GetKey(KeyCode.D))
    28.         {
    29.             transform.Translate(Vector3.right * rotateSpeed);
    30.             transform.LookAt(player.transform);
    31.         }
    32.      
    33.         if (Input.GetKey(KeyCode.A))
    34.         {          
    35.             transform.Translate(Vector3.left * rotateSpeed);
    36.             transform.LookAt(player.transform);
    37.         }
    38.     }
    39. }
    40.  
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    You need to variables, a distance, and an angle, the distance will be the distamce between the camera and the player. If you press a, add to the angle, if you press d, remove from it, then set the offset x to distance * mathf.cos(angle), and the z to distance * mathf.sin(angle), then set the position to offset + playerpos
     
  3. ReLost

    ReLost

    Joined:
    Nov 8, 2015
    Posts:
    5
    Ok, I try this, but it still doesn't work properly
    What I did wrong?
    Code (CSharp):
    1. void Start()
    2.     {
    3.         offset = transform.position - player.transform.position;
    4.     }
    5.  
    6.     void CameraFollow()
    7.     {
    8.         transform.position = player.transform.position + offset;
    9.         transform.LookAt(player.transform);
    10.     }
    11.  
    12.     void LateUpdate()
    13.     {
    14.         CameraFollow();
    15.         if (Input.GetKey(KeyCode.D))
    16.         {
    17.             angle -= 0.1f;
    18.             offset.x = offset.x * Mathf.Cos(angle);
    19.             offset.z = offset.z * Mathf.Sin(angle);
    20.             transform.position = offset + player.transform.position;
    21.         }
    22.    
    23.         if (Input.GetKey(KeyCode.A))
    24.         {
    25.             angle += 0.1f;
    26.             offset.x = offset.x * Mathf.Cos(angle);
    27.             offset.z = offset.z * Mathf.Sin(angle);
    28.             transform.position = offset + player.transform.position;
    29.         }
    30.      
    31.     }
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Don't do offset.x = offset.x *... Use a distance instead, a distance from the camera and the player, so offset.x = dist *...
    Because you're working in c, you can't set these values separately, do offset = new vector3(offset.x here, offset.y, offset.z here).
    Remove the transform.position sets from lateupdate, and move followplayer(); line to the end of lateupdate
     
  5. ReLost

    ReLost

    Joined:
    Nov 8, 2015
    Posts:
    5
    Hm.. ok, it looks better, but still doesn't work :D
    Code (CSharp):
    1. void Start()
    2.     {
    3.         offset = transform.position - player.transform.position;
    4.         dist = transform.position - player.transform.position;
    5.     }
    6.  
    7. void LateUpdate()
    8.     {
    9.        
    10.         if (Input.GetKey(KeyCode.D))
    11.         {        
    12.             angle -= 0.1f;        
    13.             offset = new Vector3(dist.x * Mathf.Cos(angle), offset.y, dist.z * Mathf.Sin(angle));          
    14.         }
    15.      
    16.         if (Input.GetKey(KeyCode.A))
    17.         {          
    18.             angle += 0.1f;          
    19.             offset = new Vector3(dist.x * Mathf.Cos(angle), offset.y, dist.z * Mathf.Sin(angle));      
    20.         }
    21.         CameraFollow();
    22.     }
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    The distance shouldn't be a vector 3,it should be a float.
     
  7. ReLost

    ReLost

    Joined:
    Nov 8, 2015
    Posts:
    5
    Thank You very much!
    It works! Thanks :)
     
  8. ReLost

    ReLost

    Joined:
    Nov 8, 2015
    Posts:
    5
    Wait, wait wait
    I noticed a one more little problem with this:
    I start the scene and when a first press "A" or "D" the camera is going to left side of the ball, later its work properly.
    What's the reason of this?
    The code again:
    Code (CSharp):
    1.  void Start()
    2.     {      
    3.         offset = transform.position - player.transform.position;
    4.         dist = Vector3.Distance(transform.position, player.transform.position);
    5.     }
    6.  
    7.     void LateUpdate()
    8.     {
    9.        
    10.         if (Input.GetKey(KeyCode.D))
    11.         {                          
    12.             angle -= 0.02f * Time.deltaTime*100;    
    13.             offset = new Vector3(dist * Mathf.Cos(angle), offset.y, dist * Mathf.Sin(angle));          
    14.         }
    15.      
    16.         if (Input.GetKey(KeyCode.A))
    17.         {          
    18.             angle += 0.02f * Time.deltaTime*100;
    19.             offset = new Vector3(dist * Mathf.Cos(angle), offset.y, dist * Mathf.Sin(angle));      
    20.         }
    21.         CameraFollow();
    22.     }