Hi!
So I’ve been trying for a while now to make my camera script zoom smoother since right now it can be very jumpy due to the nature of the script and I’d love some help to try and fix that.
using UnityEngine;
using System.Collections;
public class cameraScript : MonoBehaviour {
Transform Player1;
Transform Player2;
public float maxSize = 10;
public float minSize = 5;
public float xMax;
public float xMin;
public float yMax;
public float cameraZoomModifier = 1.8f;
public float xOffset;
public float playerDist;
Vector3 cameraCenter;
PositionChecker posCheck;
void Start(){
posCheck = this.GetComponent<PositionChecker> ();
playerDist = Vector2.Distance(Player1.transform.position, Player2.transform.position);
}
void Update () {
Player1 = posCheck.GetPos (1);
Player2 = posCheck.GetPos (posCheck.GetLength());
playerDist = Vector2.Distance(Player1.transform.position, Player2.transform.position);
cameraCenter = Vector3.Lerp (Player1.transform.position, Player2.transform.position, 0f);
this.transform.position = new Vector3(Mathf.Clamp(cameraCenter.x, xMin, xMax) + xOffset, Mathf.Clamp(cameraCenter.y, -yMax, yMax), -10);
this.GetComponent<Camera>().orthographicSize = playerDist/cameraZoomModifier;
this.GetComponent<Camera>().orthographicSize = Mathf.Clamp(this.GetComponent<Camera>().orthographicSize, minSize, maxSize);
}
}
The game “support” 4 players, Player1 being the one in the lead of the race and Player2 being the one in the far back.
using UnityEngine;
using System.Collections;
public class PositionChecker : MonoBehaviour {
//Names of the player objects
public string[] objectNames;
Transform[] players;
int dnf = 0;
// Use this for initialization
void Start () {
players = new Transform[objectNames.Length];
for (int i = 0; i < objectNames.Length; i++) {
players _= GameObject.Find (objectNames *).transform;*_
* }*
* }*
* // Update is called once per frame*
* void Update () {*
* for (int i = 0; i < players.Length; i++) {*
* if (i == 0) {*
_ } else if (players .position.x > players [i - 1].position.x){
Transform j = players*;
Transform k = players[i - 1];
players [i - 1] = j;
players = k;
}
}
}*_
* //Currently used in the rubber banding to give more stamina to the last players*
* public float GetLead(Transform obj){*
* float lead = players[0].position.x - obj.position.x;*
_ /if (lead < 1)
lead = 1;
Mathf.Clamp (lead, 1, 10);/_
* return Mathf.Abs(lead) / 2;*
* }*
* //Get the Transform of a player (1 - Max Length)*
* public Transform GetPos(int pos){*
* return players [pos - 1];*
* }*
* //Get Lenght of players[]*
* public int GetLength(){*
* return players.Length - dnf;*
* }*
* //Add one to DNF (Did Not Finish)*
* public void DNF(){*
* dnf += 1;*
* }*
* public int GetDNF(){*
* return dnf;*
* }*
}
Now, in my player controller script I have a function that checks if that player is out of bounds and then kick him/her from the race and calling DNF() in the position checker script.
Here comes the problem, whenever one player gets kicked out the camera makes a jump since the new player that is in the back is further ahead than the player that got kicked. I’d like this transition to become smoother and not just jump forward.
If someone could help me out it would be very appreciated!