- Home /
Camera Reset! please help me to solve this.
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);
}
}
Answer by luci1B · Apr 17, 2014 at 10:27 AM
Hi,
You can create two variables Vector3 initPosition and Quaternion initRotation
and set them in the Start function
initPosition = mainCamera.position;
initRotation = mainCamera.rotation;
and in your cameraReset function do
mainCamera.position = initPosition;
mainCamera.rotation = initRotation;
Hope it works.
initPosition = mainCamera.position;
initPosition = mainCamera.rotation;
Should be
initPosition = mainCamera.position;
initRotation = mainCamera.rotation;
This could be what you're looking for, but I would use 2 empty gameobjects as parent of your "player" object (if you have one) so that movement of the camera is always relative to the player.
Thanks for your reply.
private Vector3 initPosition; private Vector3 initRotation;
void Start()
{
sniper$$anonymous$$ode = false;
initPosition = mainCamera.position;
initRotation = mainCamera.rotation;
}
void cameraReset()
{
camReset=!camReset;
mainCamera.transform.position = newVector3(0,0,0);
mainCamera.transform.Translate(0,0,0);
mainCamera.position = initPosition;
mainCamera.rotation = initRotation;
}
I did this and am getting an following errors.
Cannot implicitly convert 'UnityEngine.Quaternion' to 'UnityEngine.Vector3'
Am not sure.. How to parse values. Thanks a lot for your time. Hope you can can find my mistake. Thanks.
It is just a plain backdrop image and camera pans in that image(Backdrop). I did tried this codes, But in zoom functions now it is not only panning as per the tilt function and also normal mode it is not been reset either..
Answer by superlevani · Apr 17, 2014 at 10:17 AM
you can save normal position in Vector3, when switching to sniper mode and then set transform.position to this Vector3.
Thanks for your Answer. Can you write that script please? Am learning c# and would highly appreciate if you can explain it with an example. Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613142926im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to Math.f Clamp this 0 Answers
simple clamp camera translate on Y axis only 2 Answers
Limiting Rotation x of Main Camera 1 Answer
Camera Zoom script stops ability to pan 1 Answer
How to make camera position relative to a specific target. 1 Answer