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. Dismiss Notice

Rotate camera with object

Discussion in 'Scripting' started by abeyers, Mar 15, 2018.

  1. abeyers

    abeyers

    Joined:
    Mar 5, 2018
    Posts:
    8
    Hello, let me start by saying that i am VERY new to programming and don't know much. I am working on a project for my intro to programming class and have run into an issue. I have a sphere that I have established as the player for a maze game I am creating. It is pretty self explanatory, the sphere is the player and it has to make its way to the end of the maze. My issue has to do with when the player turns a corner. I have the camera set up to follow my player, which it does all the way through, but it doesn't rotate with the player when it turns a corner. The current code I have is below. Thanks!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraController : MonoBehaviour {
    6.  
    7.     public GameObject player;
    8.  
    9.  
    10.     private Vector3 offset;
    11.  
    12.     void Start ()
    13.     {
    14.         offset = transform.position - player.transform.position;
    15.     }
    16.  
    17.     void LateUpdate ()
    18.     {
    19.         transform.position = player.transform.position + offset;
    20.     }
    21. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    How do you move your player, out of curiosity?
     
  3. abeyers

    abeyers

    Joined:
    Mar 5, 2018
    Posts:
    8
    With the W,A,S,D keys
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    lol, cool. Sorry, I should have been more specific. Do you move the transform or a rigidbody? :)
     
  5. abeyers

    abeyers

    Joined:
    Mar 5, 2018
    Posts:
    8
    Rigidbody
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You can try this.. hopefully there isn't a bug, as I just updated it to remove movement code I was using and change variables to post a cleaner example :)
    Just drag n drop the rigidbody of the player in the inspector.
    Code (csharp):
    1.  
    2. [SerializeField]
    3.     Rigidbody rb;
    4.     Quaternion lookrot;
    5.     void Awake()
    6.     {
    7.         lookrot = Quaternion.LookRotation(transform.forward);
    8.     }
    9.     void FixedUpdate()
    10.     {
    11.         if (rb.velocity != Vector3.zero)
    12.         {
    13.             Vector3 fwd = rb.transform.InverseTransformDirection(rb.velocity);
    14.  
    15.             lookrot = Quaternion.LookRotation(fwd.normalized);
    16.         }
    17.         transform.rotation = Quaternion.RotateTowards(transform.rotation, lookrot, 270f * Time.deltaTime);
    18.         transform.position = rb.position - transform.forward * 3f + Vector3.up * 1f; // some offset, behind & above the sphere.
    19.     }
    There is some 'turning time' included, which isn't necessary.. it could be immediate. Plus, this doesn't care if you're actually going around a corner or not, it's only based on your direction --- it just puts the camera behind your last movement direction*.
     
  7. abeyers

    abeyers

    Joined:
    Mar 5, 2018
    Posts:
    8
    I just put it in and it doesn't seem to work. If it helps, this is the code I use to move my player. Thanks again for your help!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed;
    8.  
    9.     private Rigidbody rb;
    10.  
    11.     void Start ()
    12.     {
    13.         rb = GetComponent<Rigidbody>();
    14.     }
    15.  
    16.     void FixedUpdate ()
    17.     {
    18.         float moveHorizontal = Input.GetAxis ("Horizontal");
    19.         float moveVertical = Input.GetAxis ("Vertical");
    20.  
    21.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    22.  
    23.         rb.AddForce (movement * speed);
    24.     }
    25. }
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You put my code on the camera and then dragged n dropped the ball to the slot in the inspector and it didn't work?
     
  9. abeyers

    abeyers

    Joined:
    Mar 5, 2018
    Posts:
    8
    Nevermind I had an error when copying. It’s fine now
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Oh okay, that's good. Is it working how you'd like?
     
  11. Hotspur76

    Hotspur76

    Joined:
    Sep 26, 2020
    Posts:
    5
    Hi Methos, I have exactly the same issue, but when I add your code and then move the sphere the camera moves around wildly. What am I doing wrong? Do I replace my camera code (same as the top of this feed) completely with your code or add yours underneath? I have tried both and neither work?
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    @Hotspur76 : If you're moving with a rigidbody, you can use the code provided. It was long ago that I posted it. If your camera is moving around wildly, it could be because it's a child of your player. Ensure it's not a child and try, if you haven't yet solved it. Sorry, I haven't been around the forums in quite a while.