- Home /
SmoothFollow2D - Standard Assets(mobile)
im trying to modify the sideScrollSetup scene in the Standard Assets(mobile) package, the SmoothFollow2D script.
I want the camera to stop following the player when a certain point is reached and vice versa. The if statement i added works with one little problem. The camera stops fine, and when u move back from that certain point the camera follows the player again however its not a smooth transition. It sort of bounces once.
thanks
var target : Transform;
var smoothTime = 0.3;
private var thisTransform : Transform;
private var velocity : Vector2;
function Start() {
thisTransform = transform;
}
function Update() {
thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y,
target.position.y, velocity.y, smoothTime);
**if(target.position.x < 19 && target.position.x > -15) {**
thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x,
target.position.x, velocity.x, smoothTime);
}
}
Answer by samz · Apr 09, 2012 at 07:13 AM
0o0o0o thnx for your massive code lol. But i fixed it :D Well i didnt do much, just changed the smoothTime value to 0.01
Well. With my script, your camera will follow your player only if he is in a special area. Please put your subject as resolved by saying my answer is correct.
Answer by DJSwiti · Apr 08, 2012 at 06:11 PM
Hello. I can show you what I made, because i had the same problem. With this script, the camera will follow your player, but if he is X < 4 and X > 11 and Z < 1 and Z > 8
using UnityEngine;
using System.Collections;
public class Follow2D_xy : MonoBehaviour
{
public Transform _Player;
public float _cameraSpeed = 10;
public float cam = 3;
int iamout_x = 0;
int iamout_z = 0;
bool locked;
static public float setto_x = 11;
static public float setto_z = 8;
Transform _transform;
void Awake()
{
_transform = transform;
}
void Start(){
locked = true;
}
void Update ()
{
Vector3 newPos = _transform.position;
Vector3 pos = _transform.position;
if(locked == true){
newPos.x = _Player.position.x;
newPos.z = _Player.position.z;
pos.x = Mathf.SmoothStep(setto_x, newPos.x, cam * Time.deltaTime);
pos.z = Mathf.SmoothStep(setto_z, newPos.z, cam * Time.deltaTime);
_transform.position = pos;
if((pos.x >= 7.8) && pos.z >= 3.8)
locked = false;
}
if((newPos.x >= 4) && (newPos.x <= 11) && (iamout_x == 0) && (locked == false)){
newPos.x = Mathf.SmoothStep(newPos.x, _Player.position.x, _cameraSpeed * Time.deltaTime);
_transform.position = newPos;
}
else if((newPos.x <= 4) && (locked == false)){
iamout_x = 1;
newPos.x = 4;
_transform.position = newPos;
if(_Player.position.x >= 4)
iamout_x = 0;
}
else if((newPos.x >= 11) && (locked == false)){
iamout_x = 1;
newPos.x = 11;
_transform.position = newPos;
if(_Player.position.x <= 11)
iamout_x = 0;
}
if((newPos.z >= 0) && (newPos.z <= 8) && (iamout_z == 0) && (locked == false)){
newPos.z = Mathf.SmoothStep(newPos.z, _Player.position.z, _cameraSpeed * Time.deltaTime);
_transform.position = newPos;
}
else if((newPos.z <= 0) && (locked == false)){
iamout_z = 1;
newPos.z = 0;
_transform.position = newPos;
if(_Player.position.z >= 0)
iamout_z = 0;
}
else if((newPos.z >= 8) && (locked == false)){
iamout_z = 1;
newPos.z = 8;
_transform.position = newPos;
if(_Player.position.z <= 8)
iamout_z = 0;
}
}
}
Hope this helped.
Your answer
Follow this Question
Related Questions
How to Camera Rotation when UI is up? 0 Answers
Change a float value with the horizontal axis? 1 Answer
Camera bounds 0 Answers
Trouble with logic related to my camera smoothing 0 Answers
Rotate Camera to the object 0 Answers