Hi guys,
I have the object and it throws some distance and it stops, when it stops i have to move the camera.
Like in this game
How can i do this ? Please give me idea on this script (c#).
Hi guys,
I have the object and it throws some distance and it stops, when it stops i have to move the camera.
Like in this game
How can i do this ? Please give me idea on this script (c#).
Check if player is moving (rigidbody.velocity or checking the changes in position every frame)
if he doesn’t move, set the camera position with lerp (for smoothness)
Done
Just drop the camera on your gameobject so it gets a child of it, and follows it automatically.
He wants, that there are certain times, when the camera isn’t moving (specifically when the character itself moves)
I am not sure how you are checking when the character stops, but once he does, you can use the following code to smoothly move the camera.
Place this on your camera and call MoveCamera(new Vector2(x, y)):
public class SlideCamera : MonoBehaviour {
public float duration = 2f;
float time = 0f;
bool move = false;
Vector3 moveTo = Vector3.zero;
public void MoveCamera(Vector2 position){
move = true;
moveTo = position;
moveTo.z = transform.position.z;
}
void Update(){
if(move){
transform.position = Vector3.Slerp(transform.position, moveTo, time);
time += Time.deltaTime / duration;
if(transform.position == moveTo){
move = false;
time = 0f;
}
}
}
}
Then this is an example of how to use it if you’re moving via rigidbody:
public class PlayerMoved : MonoBehaviour {
Rigidbody2D rb;
Camera cam;
void Start(){
rb = GetComponent<Rigidbody2D>();
cam = Camera.main;
}
void FixedUpdate(){
if(rb.velocity.magnitude == 0f){
cam.GetComponent<SlideCamera>().MoveCamera(transform.position);
}
}
}