- Home /
Question by
FGamesCreation · May 06, 2020 at 10:59 PM ·
androidunity 5
Camera follow jitter when player move back on platform
Have a script with camera follow the player, was trying to mae it more smooth etc.
everything would work fine, but when player jump on moving platform (platform moving towards player) and if player go forward, camerra jitter a bit, but when player go back, basically every platform that was going towards me stands still and camera shakes,
Not sure what to do here please advise. Thank you.
Platformscript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class platform : MonoBehaviour {
public float Platformspeed;
private void Update()
{
transform.Translate(Vector2.left * Platformspeed);
}
}
CameraFollowScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMario : MonoBehaviour {
public GameObject followObject;
public Vector2 followOffset;
public Vector2 thershold;
public float speed = 3f;
private Rigidbody2D rb;
// Use this for initialization
void Start () {
thershold = calculateThershold();
rb = followObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void LateUpdate () {
if (rb != null)
{
Vector2 follow = followObject.transform.position;
float xDifference = Vector2.Distance(Vector2.right * transform.position.x, Vector2.right * follow.x);
float yDifference = Vector2.Distance(Vector2.up * transform.position.y, Vector2.up * follow.y);
Vector3 newPosition = transform.position;
if(Mathf.Abs(xDifference)>= thershold.x)
{
newPosition.x = follow.x;
}
if(Mathf.Abs(yDifference) >= thershold.y)
{
newPosition.y = follow.y;
}
float moveSpeed = rb.velocity.magnitude > speed ? rb.velocity.magnitude : speed;
Vector3 desiredPosition = Vector3.MoveTowards(transform.position, newPosition, moveSpeed *
Time.deltaTime);
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, speed);
transform.position = smoothedPosition;
}
}
private Vector3 calculateThershold()
{
Rect aspect = Camera.main.pixelRect;
Vector2 t = new Vector2(Camera.main.orthographicSize * aspect.width / aspect.height, Camera.main.orthographicSize);
t.x -= followOffset.x;
t.y -= followOffset.y;
return t;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.blue;
Vector2 border = calculateThershold();
Gizmos.DrawWireCube(transform.position, new Vector3(border.x * 2, border.y * 2, 1));
}
}
Comment
Your answer
