Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kevinspawner · Apr 17, 2014 at 09:49 AM · cameraclamp

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);        

 }

 
 
 

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xortrox · Apr 21, 2014 at 03:29 AM 0
Share
 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.

avatar image kevinspawner · Apr 21, 2014 at 04:11 AM 0
Share

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.

avatar image xortrox · Apr 21, 2014 at 04:13 AM 0
Share

initRotation should be a Quaternion and not a Vector3

avatar image kevinspawner · Apr 21, 2014 at 09:09 AM 0
Share

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..

avatar image kevinspawner · Apr 22, 2014 at 10:34 AM 0
Share

Can some one help me to fix it please. Thanks.

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kevinspawner · Apr 21, 2014 at 08:18 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges