Hello,
I just recently (about 3 Weeks now) started learning unity3D and now have my first need for assistance.
I used the standard assets FirstPersonCharacter to create a GameObject with which I can move around in my scene. What I also have are different GameObject NPC’s. What I would like to accomplish is that if i click on one NPC that NPC is “selected” and the camera now follows that selected NPC around. If you right click you then “detach” from the NPC and can play as a first person character again.
I can already “select” a NPC but don’t know how to change the camera except for maybe adding a child camera to every NPC?
void Update() {
if (Input.GetMouseButtonDown (0)) {
hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, 300);
if (hit) {
if (hitInfo.transform.gameObject.tag == "NPC") {
// CHANGE CAMERA
}
}
}
}
Any help is much appreciated.
There are two basic ways to do it, and which one you want depends on how you think you might progress in the future.
A: Attach cameras to every guy. Will allow you to have multiple cameras running simultaneously in the future (think picture-in-picture). On the other hand, it will be more complex if you want to change the camera properties at some point in the future, and you may risk performance problems if there are any bugs with the activation/deactivation code.
B: Move the main camera. Will allow smooth transitions from one to the next if you want to do that, and will make it easy to change camera properties in the future.
They’re very different strategies, and I’ll help with whichever sounds more like what you’ll want for your game, but they both have advantages so I can’t absolutely recommend one over the other.
Thank you for your help. Personally I think B sounds like the better also probably easier option. If you could help then thank you very much.
I would generally lean towards B as well. So here’s what you want to do:
-
Put a script on your main camera. Let’s call it CameraMover.
-
Create a GameObject as a child of every enemy that will represent where the camera would be. Attach a script to this object; let’s call it VirtualCamera. The VirtualCamera will move as if it were a real camera when it’s active.
-
In whatever script you have that controls the NPCs, make a public VirtualCamera member, and make sure it points to that NPC’s VC.
-
In CameraMover, add this:
public static CameraMover main; //this is a singleton, by the way
void Awake() {
main=this;
}
public VirtualCamera overrideCamera = null;
public VirtualCamera defaultCamera;
void LateUpdate() {
if (overrideCamera != null) {
transform.position = overrideCamera.transform.position;
transform.rotation = overrideCamera.transform.rotation;
}
else {
transform.position = defaultCamera.transform.position;
transform.rotation = defaultCamera.transform.rotation;
}
}
Make sure that defaultCamera points to a VirtualCamera which is attached to your player.
- Now, once you have a reference to your NPC, you can set its camera to be in focus like this:
YourNPCScript thisNPC = hit.collider.GetComponent<YourNPCScript>();
CameraMover.main.overrideCamera = thisNPC.virtualCamera;
And then to return to the main view:
CameraMover.main.overrideCamera = null;