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

Move cam up when character climbs

Discussion in 'Scripting' started by neatgadgets, Jul 10, 2014.

  1. neatgadgets

    neatgadgets

    Joined:
    May 13, 2014
    Posts:
    73
    I have some code that makes the camera follow the player (without making it a child). The problem with the code is that it only works if the player stays at the same Y position. If it climbs steps I get a lovely view of the players feet.

    Any ideas how to amend this code to get it to follow in the y direction as well?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class cameraFollow : MonoBehaviour {
    5.  
    6.     public AnimationCurve FollowCurve = AnimationCurve.Linear(0,1,10,50);
    7.     private GameObject cam;
    8.     private Vector3 offset;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         cam = Camera.main.gameObject;
    13.         offset = transform.position - cam.transform.position;
    14.  
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void FixedUpdate () {
    19.         Vector3 currentPos = transform.position - offset;
    20.         currentPos.y = -offset.y;
    21.         Vector3 camPos = cam.transform.position;
    22.  
    23.         float distance = Vector3.Magnitude (currentPos - camPos);
    24.         distance = FollowCurve.Evaluate (distance);
    25.         cam.transform.position = Vector3.MoveTowards (camPos, currentPos, distance * Time.fixedDeltaTime);
    26.  
    27.     }
    28. }
    29.  
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Did you try something along the lines of ~ ?
    Code (CSharp):
    1. currentPos.y = -offset + transform.position.y;
    ot: I like your picture c:
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    or just remove line 20...
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Yeah, assuming that it was not intented to subtract the Y different twice.
     
  5. neatgadgets

    neatgadgets

    Joined:
    May 13, 2014
    Posts:
    73
    Hey thanks for the replies guys. I'll give it a go shortly after I try and fix another issue. And the pic, thanks Maggichan. :)
     
    Magiichan likes this.
  6. neatgadgets

    neatgadgets

    Joined:
    May 13, 2014
    Posts:
    73
    Yep removing that line worked. Is it possible to swing the camera back to behind the character?
     
  7. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Code (CSharp):
    1. float distance = 5f;
    2. Vector3 currentPos = (transform.position - offset) + (cam.transform.position - transform.position).normalized * distance;
    It basically subtracts the player's position from the camera's position,
    and normalizes. Which give you the direction of towards the camera relative to the player.
    We multiply it by the distance offset that you want.
    And add it with the currect camera position.

    Watch this Vector Maths tutorial if you want to learn more about 3D space calculations n such.
     
    Last edited: Jul 14, 2014