- Home /
Camera Following character that teleported
I've already created a "smooth" camera script to follow the character, the thing is i've added a teleport spot that teleports the character to another place (x,y) but the camera also "teleports" with him i want the camera to follow up like showing the road to him is it possible?
It's a 2D game btw, thanks!
Script:
using UnityEngine; using System.Collections;
public class CameraController : MonoBehaviour {
public Transform Player;
public Vector2
Margin,
Smoothing;
public BoxCollider2D Bounds;
private Vector3
_min,_max;
public bool isFollowing { get; set; }
public void Start()
{
_min = Bounds.bounds.min;
_max = Bounds.bounds.max;
isFollowing = true;
}
public void Update()
{
var x = transform.position.x;
var y = transform.position.y;
if (isFollowing) {
if (Mathf.Abs(x - Player.position.x) > Margin.x)
x = Mathf.Lerp(x, Player.position.x, Smoothing.x * Time.deltaTime);
if (Mathf.Abs (y - Player.position.y) > Margin.y)
y = Mathf.Lerp(y, Player.position.y, Smoothing.y * Time.deltaTime);
}
var cameraHalfwidth = camera.orthographicSize * ((float)Screen.width / Screen.height);
x = Mathf.Clamp(x, _min.x + cameraHalfwidth, _max.x - cameraHalfwidth);
y = Mathf.Clamp(y, _min.y + camera.orthographicSize, _max.y - camera.orthographicSize);
transform.position = new Vector3 (x, y, transform.position.z);
}
}
Answer by yashpal · Oct 30, 2014 at 08:50 AM
Hello @StatecLata
You can directly use script which is provided by unity named "SmoothFollow". You will find this script by following "Assets" tab -> "Import Package" -> "Scripts". And Add "SmoothFollow.js" this script attach to camera and give player as target.
Hope this will help you.
Thanks for your tip @yashpal although you're not helping me with the basic issue
@StatecLata, you are using clamp in your script so it is always clamp value of your camera movement. so your character move inside your camera view than camera is follow player.
If you don't use this code then camera follow your player
var cameraHalfwidth = camera.orthographicSize * ((float)Screen.width / Screen.height);
x = $$anonymous$$athf.Clamp(x, _$$anonymous$$.x + cameraHalfwidth, _max.x - cameraHalfwidth);
y = $$anonymous$$athf.Clamp(y, _$$anonymous$$.y + camera.orthographicSize, _max.y - camera.orthographicSize);
or you can put condition means
if(!PlayerTranstortedbool) // Default PlayerTranstortedbool = false
{
var cameraHalfwidth = camera.orthographicSize * ((float)Screen.width / Screen.height);
x = $$anonymous$$athf.Clamp(x, _$$anonymous$$.x + cameraHalfwidth, _max.x - cameraHalfwidth);
y = $$anonymous$$athf.Clamp(y, _$$anonymous$$.y + camera.orthographicSize, _max.y - camera.orthographicSize);
}
when player transported set PlayerTranstortedboolflag true
Hope this might help.
@StatecLata, I use your code edit it and made one demo scene. it works fine when player teleport.
Steps to implement are following
1) download package from this link
2) import in new project.
3) open and run Scene "CameraFollow"
4) click any where in game screen to teleport player (RedCube).
Hope it is what you are looking for.
@yashpal i don't want the camera to follow the player into teleport, i want the camera to walk to the player mate!
Your answer
Follow this Question
Related Questions
Camera Transform.Position (teleport) 2 Answers
How can I make a character disappear from view from one side to the other? 1 Answer
How can I program a 'wrap-screen'? 2 Answers
How to make camera position relative to a specific target. 1 Answer
Issues with player prefabs when I try to teleport it 1 Answer