- Home /
Switching from 3rd person to 1st person (Android)
Hey all,
I was wondering if anyone had an idea for me to switch from 3rd person camera to 1st person camera and vice-versa. I'm using Android so my controls will be based on touch.
Something that I'm trying to keep in mind is that when the character is in 3rd person, the controls will be based on UI surrounding the character. In 1st person the UI controls will only be rotation on either side of the screen.
Answer by Nick4 · Jan 11, 2014 at 07:57 AM
var fpsCamera : Camera;
var thirdPersonCam : Camera;
var switchEnabled : boolean = false;
function Update(){
if (Input.touches.Length > 0)
{
for(int i = 0; i<Input.touchCount; i++)
{
if(this.guiTexture.HitTest(Input.getTouch(i).position))
{
if (switchEnabled == false)
{
fpsCamera.enabled = false;
thirdPersonCam.enabled = true;
switchEnabled = true;
}
else
{
mainCamera.enabled = true;
thirdPersonCam.enabled = false;
switchEnabled = false;
}
}
}
}
}
Attach this to the gui texture you want to switch cameras with and attach your cameras to these variables. That should do it. Not tested but let me know what's wrong if it doesn't work. Good luck.
And sorry about that weird look. This website's code input doesn't allow me to shape it properly.
This seems to not work.. I tried suiting it to my needs but I'm having an issue with this part.
for(int i = 0; i<Input.touchCount; i++)
{
if(this.guiTexture.HitTest(Input.getTouch(i).position))
Unity is having a trouble with (i),int,getTouch, and, Position. I'm a bit confused as to why to be honest.
Grrr having issues all over the place....
#pragma strict
var firstPerson : Camera;
var thirdPerson : Camera;
var Active : boolean = false;
function Update()
{
if(Input.GetTouch == false)
{
firstPerson.enabled == false;
thirdPerson.enabled == true;
Active == true;
}
else if(Input.GetTouch == true)
{
firstPerson.enabled == true;
thirdPerson.enabled == false;
Active == false;
}
}
Not sure why unity is having an issue with my script, seems I'm either missing something or ... I'm not sure actually...
Assets/3rdto1st.js(14,37): BCE0034: Expressions in statements must only be executed for their side-effects.
Assets/3rdto1st.js(15,24): BCE0034: Expressions in statements must only be executed for their side-effects.
Assets/3rdto1st.js(21,61): BCE0034: Expressions in statements must only be executed for their side-effects.
Assets/3rdto1st.js(22,48): BCE0034: Expressions in statements must only be executed for their side-effects.
I left the HitTest because it was having an issue with int and (i) for some reason..
ok so i found out i was using == ins$$anonymous$$d of =. so there arent any errors, however the gui does nothing when i touch it on my tablet. any ideas?
$$anonymous$$y uncle helped me and we figured it out !!
Here is my script for anyone reading my questions.
#pragma strict
var firstPerson : Camera;
var thirdPerson : Camera;
var useThirdPerson = false;
private var oldUseThirdPerson;
private var touched = false;
function UpdateCameras()
{
firstPerson.enabled = !useThirdPerson;
thirdPerson.enabled = useThirdPerson;
}
function CheckTouch() {
// Assume the screen is not touched
touched = false;
// Are any fingers on the screen
if (Input.touchCount > 0) {
touched = true;
}
}
function Start()
{
UpdateCameras();
}
function OnGUI() {
if (GUI.Button(Rect(1100, 40, 80, 80), "SwitchCam")) {
useThirdPerson = !useThirdPerson;
}
// Only update if things have changed.
if (useThirdPerson != oldUseThirdPerson)
{
oldUseThirdPerson = useThirdPerson;
UpdateCameras();
}
}
Hope it helps you guys, Happy Unity'ing
Your answer
Follow this Question
Related Questions
Android H/w Camera Button 1 Answer
Control the camera with a half of the touch screen 0 Answers
Phone camera autofocus 0 Answers
Smooth Follow Camera Rotate on Z-Axis? 2 Answers