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

Camera following behind player help

Discussion in 'Scripting' started by Michael_93, Jul 1, 2014.

  1. Michael_93

    Michael_93

    Joined:
    Jan 12, 2014
    Posts:
    95
    I've been trying to do this for about 2 days now and I'm completely stuck. I want to camera to always sit behind my player. The Player itself is always moving in one direction and the player changes the angle the player moves. Any help is greatly appreciated I'll be watching this thread closely so if you don't understand what I'm saying I'll get back fast.
    Even if you aren't completely sure what to do leave a suggestion.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class cameraFollow : MonoBehaviour {
    5.  
    6.     private float angle;
    7.     public GameObject Player;
    8.     private Vector3 offset;
    9.     public Vector3 distance;
    10.        
    11.     void Start () {
    12.    
    13.         offset = transform.position;
    14.  
    15.         }
    16.  
    17.    
    18.     void LateUpdate () {
    19.  
    20.         transform.LookAt (Player.transform.position);
    21.         distance = (Player.transform.position + offset);
    22.     //    transform.RotateAround(Player.transform.position,Vector3.up,180);
    23.  
    24.         transform.position=  distance;
    25.  
    26.    
    27.     //    Debug.DrawRay(transform.position, location, Color.red);
    28.         //rotate
    29.        
    30.     }
    31. }
    32. //transform.Rotate((new Vector3(x,0,z)), Time.deltaTime, Space.World);
    33. //transform.Translate ((new Vector3 (x,y,z) * Time.deltaTime)*constantSpeed);
    Thank you for any help
     
  2. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Did you try to parent the camera to the player? So if the player moves, the camera will move automatically.
    It might not be super smooth though ...
     
  3. ian_sorbello

    ian_sorbello

    Joined:
    Jul 1, 2014
    Posts:
    6
    That's what I've done to achieve this simply.
    Just drag the camera to the object you want to follow, so it's the camera's parent.

    Transform the camera so it's slightly behind the character and pointing in the 'forward' direction. When your character moves/rotates, the camera rotates around the parents local co-ords.

    The only negative is that it's pretty raw. If you want the camera to smoothly move, you'll need to add some scripts to introduce dampening etc. Also what is your strategy for collision detection? Ensuring that the camera does not dip down below ground/walls etc. If you add rigid body to the entire object tree and attaching a collider - this can work automatically, though I still find weird issues from time to time. (Camera dipping down just below the ground).
     
  4. Michael_93

    Michael_93

    Joined:
    Jan 12, 2014
    Posts:
    95
    The thing is my character doesn't actually rotate, I may have done the movement wrong. I broke up the movement into four different quadrants using if statements. Then it moves the character with translate, it's weird the way I did it and I'll try to do it a different way with another script. I'll post what I did if you want.
     
  5. Michael_93

    Michael_93

    Joined:
    Jan 12, 2014
    Posts:
    95
    Thanks for the help I'm still not done yet but I'm much closer. However the rotation keeps on resetting am I missing something?
    Here is a bit of my code after all my calculations


    Edit
    I feel like an idiot I just learnt that Vector's are in local space and not word space.


    Code (CSharp):
    1.  
    2.         float tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle;
    3.         Quaternion target = Quaternion.Euler(0, tiltAroundY, 0);
    4.         transform.rotation = Quaternion.Slerp(transform.rotation, target, turnSpeed);
    5.         transform.rotation = transform.rotation;
    6.         moveDirection = ((new Vector3 (x, y, z)*Time.deltaTime)*constantSpeed);
    7.         moveDirection.y -= gravity * Time.deltaTime;
    8.  
    9.  
    10.         Debug.DrawRay(transform.position, new Vector3(x,y,z), Color.red,3.0F);
    11.         transform.Translate ((new Vector3 (x,y,z) * Time.deltaTime)*constantSpeed);
     
    Last edited: Jul 1, 2014
  6. ian_sorbello

    ian_sorbello

    Joined:
    Jul 1, 2014
    Posts:
    6
    Are you simulating flight here? I can see modifications to the y vector based on gravity over time.
    Also not sure why you are doing line 5? This won't actually change anything - you're assigning the same value to itself.

    As for the math - not completely sure I'm following... That in itself is a different problem. What would be interesting to know is if adding a camera to the parent works at the very least (your rotation probs aside).

    ian.
     
  7. Michael_93

    Michael_93

    Joined:
    Jan 12, 2014
    Posts:
    95
    I figured it out with after a bit of guidance