Quantcast
Channel: Questions in topic: "follow"
Viewing all articles
Browse latest Browse all 536

2D Camera follow player up but not down

$
0
0
Hey everyone. I'm using the code from the Unity 2D game example tutorial for my camera movement. Right now it follow the character in any direction. What I want is that if my character moves up in the Y direction, the camera follows it, but if it moves down in the Y, the camera doesn't follow. Basically I want the bottom of the camera to be the kill trigger, so the player should never fall. I've tried several things to no success, any thoughts? using UnityEngine; using System.Collections; public class CameraFollow : MonoBehaviour { public float xMargin = 0f; public float yMargin = 0f; public float xSmooth = 0f; public float ySmooth = 0f; public Vector2 maxXAndY; public Vector2 minXAndY; private Transform player; void Awake () { player = GameObject.FindGameObjectWithTag("Player").transform; } bool CheckXMargin() { return Mathf.Abs (transform.position.x - player.position.x) > xMargin; } bool CheckYMargin() { return Mathf.Abs (transform.position.y - player.position.y) > yMargin; } void FixedUpdate () { TrackPlayer(); } void TrackPlayer () { float targetX = transform.position.x; float targetY = transform.position.y; if(CheckXMargin()) targetX = Mathf.Lerp (transform.position.x, player.position.x, xSmooth * Time.deltaTime); if(CheckYMargin()) targetY = Mathf.Lerp (transform.position.y, player.position.y, ySmooth * Time.deltaTime); targetX = Mathf.Clamp (targetX, minXAndY.x, maxXAndY.x); targetY = Mathf.Clamp (targetY, minXAndY.y, maxXAndY.y); transform.position = new Vector3(targetX, targetY, transform.position.z); } }

Viewing all articles
Browse latest Browse all 536

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>