- Home /
Camera movement question
Hi, I'm trying to move the camera from a certain view one gameobject to another one based off mouse click. Ive tried a few different things but cannot seem to achieve this. There doesn't seem to be much similar in tutorials or the unity forums. If someone could point me on the right track or to something i may have missed on the forums it would be greatly appreciated. Thanks.
Answer by roamcel · Aug 11, 2011 at 12:55 PM
If I correctly understand your question, your only need is to change the camera transform.
1- add a collider to your object
2- in your object's script add the following function
void OnMouseDown() {
Camera.main.transform.position = otherobject.transform.position + cameraoffsetvector;
}
cameraoffsetvector is a vector which displaces your camera into its viewport position (otherwise it'll just cohincide with otherobject's transform)
Wow that seems much simpler then the process i was thinking about in my head. Thank you very much. Although wouldn't otherobject error out in the script?.. Ok figured i better double check first. I do get the error, so I'm assu$$anonymous$$g it was just general and I'd need to define otherobject somewhere? Basically whats happening after object load in the world view(from sql) and the camera stays on the initial spot.(before asking the question) ultimately id like it to load to one of the players objects and have the ability to click nearby objects (separated by scenes or view distance) and have the camera move to that location and do it's slow orbit around the object. Unfortunately there doesn't seem to be a lot of camera reference for me in regards to anything like this and im not the best developer. Thank you again for the input.
Ok i understand more now. Thank you again it was really helpful and so simple!
I'm sorry if it was not very clear. 'otherobject' is your local reference to the 'other' object that will s$$anonymous$$l the camera. Usually, you specify it as a public variable in your script, and you drag it in the inspector from the scene hierarchy, or you get it with something like
GameObject otherobject = GameObject.Find("myotherobjectinthescene");
In your particular case, you'll need to come up with a script that returns the gameobject you click upon, so you possibly need to:
1- put your camera swapping script on the camera or another persistent object
2- whenever you click on an object, you trigger the camera's resident script which set the 'otherobject' variable
3- call the function to swap camera