The main camera follows a ball but if the ball passes say 1200 on the x axis the camera comes to a sudden stop at 1200. I would like to start to slow the camera down at 1100 on the x axis and have it smooth to a complete stop at 1200 to avoid any sudden camera moment.
A simple approach:
If the camera is > 1100 create a factor:
(1200-CameraPosition) / 100f ← This will give you a value between 0 and 1. Add this factor to the camera movement and it will be normal speed at Distance > 100 and slower if not.
Beware: You will have to clamp the values and catch the direction before applying this factor. Else if the camera is at position 1200 and the user trying to move in the other direction, it will still use the factor 0 and therefor get the camera stuck.
Thanks for the reply. I tried to get this to function today but I must be over thinking it and it is not working as expected. Basically it’s a cannon ball that is fired from the left or right side of the camera. The camera follows the cannon ball and if it is shot from the right side of the camera, the camera waits till the cannon ball is at the center of the camera frame and then follows it. If it is to the right of the camera, then I measure the offset between the x of the cannon ball and the x of the camera and track it from behind, instead of the middle. That way it prevents the camera from snapping to the cannon balls position after it is instantiated.
ballXOffset = Camera.main.transform.position.x - transform.position.x;
adjustCamDistanceRight = (transform.position.x > Camera.main.transform.position.x)? 0f : ballXOffset;
adjustCamDistanceLeft = (transform.position.x < Camera.main.transform.position.x)? 0f : ballXOffset;
// Update is called once per frame
void Update () {
if((Camera.main.transform.position.x > -1200) && Camera.main.transform.position.x < 1200){ // The camera only moves between these points
if (isRight && transform.position.x < 1180) { // right side
Camera.main.transform.position = new Vector3(adjustCamDistanceRight + transform.position.x , 0f , -200);
}
if(!isRight && transform.position.x > -1180){
float slowDownMulti = (1200 - transform.position.x) / 200f; // modifier
float camPosX = (transform.position.x < 1200) ? (transform.position.x - ballLastPos) * slowDownMulti + ballLastPos : 1200f;
ballLastPos = transform.position.x; // store the last position
print("ball/cam distance: " + (camPosX -transform.position.x).ToString("0") + " ball x: " + transform.position.x.ToString("0") + " Cam X total movment: " + (camPosX - camLastPosition).ToString("0"));
camLastPosition = camPosX;
Camera.main.transform.position = (Camera.main.transform.position.x < 1000)? new Vector3(adjustCamDistanceLeft + transform.position.x , 0f , -200) : new Vector3(camPosX , 0f , -200);
}
}
}
The image I included show the same speed of the cannon ball every frame. I would expect the distance between frames to slow down.
line 2 and 3 I calculate if there is an offset from the cannon ball to the camera.x or not.
line 10 is where the If statement starts the camera movement is between -1200 to 1200
line 12 is the right side and works fine except it stops suddenly, which is why I am trying to slow down the stop
line 14 is the left side and where I started to test it.
line 16 is your solution, I expanded it to give it more time to slow down.
line 17 is were the slowdown code should take place until it reaches 1200.
I am getting the current transform of the ball and subtracting the last saved transform to get the total distance moved. I am then multiplying that by the slowdown percentage and adding that to the last transform. So it should get less and less till it stops at 1200?
Something is wrong though, any help would be appreciated.