Move the Main Camera with OnMouseUp

Hello guys I need to move my Main Camera when my Select level is pressed (I use OnMouseUp)… I use for destination an empty gameobject

You’ll need to reference your empty game object somehow, either by including it in your camera script as a public var, or by giving it some kind of name and pulling it in that way. The second method is perhaps more flexible, so I’ll use that one.

In your script that handles the OnMouseUp for your select level menu item, you’ll need to make a few changes. First, you’ll need to add a variable to hold the empty game object that is the camera’s target. Do that in the class definition, before your Start funtion:

Private GameObject cameraTarget;

Then, add the following line to your Start function to reference the empty game object you made:

void Start() {
  //...your other code here
  cameraTarget = GameObject.find("SelectLevelCameraTarget");
}

For that to work, you’ll need to give your empty game object the name “SelectLevelCameraTarget”, so go ahead and do that.

Then, in your OnMouseUp function:

void OnMouseUp() {
  //..anything you need to happen before camera move
  Camera.main.transform = new Vector3(Camera.main.transform.x, cameraTarget.transform.y, Camera.main.transform.z);
  //..anything you need to happen after camera move
} 

NOW - what this will do is set the camera’s Y position equal to the Y position of your SelectLevelCameraTarget. I’m assuming Y because you said you need your camera to move “up” - if you need it to move in other dimensions, you can modify accordingly. HOWEVER - this script will “jump” the camera instantly to that position. If you want to animate it “smoothly” over time, you’ll have to lerp the camera’s position. This is probably best done with a coroutine. These can seem a little bit intimidating, but they’re not so bad - there’s a great little tutorial vid here: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

To lerp the camera’s position so it animates instead of just jumping, you’ll need to change the above code to be something more like this:

void OnMouseUp() {
  //..anything you need to happen before camera move
  StartCoroutine(MoveCamera());
  //..anything you need to happen after camera move
} 

IEnumerator MoveCamera() {
  while(Vector3.Distance(Camera.main.transform.position, cameraTarget.transform.position) > 0.05f) {
    Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, cameraTarget.transform.position, Time.deltaTime)
    yield return null;
  }
}

If you want to change the speed at which the camera moves, you’ll need to create a float called “camSpeed” or something similar right above where you declare cameraTarget, and then in your coroutine multiply Time.deltaTime * camSpeed.

Hope that helps.

Some how i manage to move my game object but my game object moves toward Camera can any one correct my code and send me. please!
public class NewBehaviourScript : MonoBehaviour {

 public float speed = 1.5f;
 private Vector3 target;

 void Start () {
     target = transform.position;
 }
 
 void Update () {
     if (Input.GetMouseButtonDown(0)) {
         target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         target.z = transform.position.z;
     }
     transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
 }    

}