Greetings fellow coders,
Am basically creating a sniper mode game… Camera will be in normal and sniper mode. Which can be switched through double tap. Well, I did achieved the Zoom function and the player can tilt the device to move the camera in X and Y axis in Zoom View and once the user double tap again it will switch to Normal View.
Problem: I need to reset the camera position once the player switch back from sniper to normal mode… Currently in my code am not sure how to fix this. Please help me out. Thanks in advance. Thanks for your valuable time.
using UnityEngine;
using System.Collections;
public class Delete : MonoBehaviour
{
public Texture2D sniperTexture;
public Material sniperMat;
public bool sniperMode=false;
private bool isplaying = false;
private bool camReset = false;
private bool camResetSwitch = false;
public float maxX = 0.0f;
public float minX = 0.0f;
public float maxY = 0.0f;
public float minY = 0.0f;
public float speed = 6.0f;
public float filter= 5.0f;
private Vector3 acceleration;
public Transform mainCamera;
void Start()
{
sniperMode = false;
}
void FixedUpdate()
{
DoubleTap();
gyroMovement();
cameraReset();
}
void OnPostRender()
{
if(sniperMode)
Graphics.Blit( sniperTexture,null,sniperMat);
}
void DoubleTap()
{
for (var i=0; i< Input.touchCount; ++i)
{
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
if(Input.GetTouch(i).tapCount == 2)
{
sniperMode=!sniperMode;
Camera.main.fieldOfView = sniperMode ? 20 : 60;
isplaying = true;
camResetSwitch = false;
}
}
else
{
isplaying = false;
camResetSwitch = true;
}
}
}
void gyroMovement()
{
if(sniperMode=sniperMode)
{
acceleration = Input.acceleration;
acceleration = Vector3.Lerp (acceleration,Input.acceleration,filter*Time.deltaTime);
Vector3 dir = new Vector3 (acceleration.x,acceleration.y,0);
if(dir.sqrMagnitude > 1)
dir.Normalize();
transform.Translate(dir*speed*Time.deltaTime);
transform.Translate(dir*speed*Time.deltaTime);
var pos = transform.position;
pos.x = Mathf.Clamp(pos.x, minX,maxX);
pos.y = Mathf.Clamp(pos.y, minY,maxY);
transform.position = pos;
}
else
{
isplaying = false;
camResetSwitch = true;
}
}
void cameraReset()
{
camReset=!camReset;
mainCamera.transform.position = new Vector3(0,0,0);
mainCamera.transform.Translate(0,0,0);
}
}