• 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 Bentoon · Mar 03, 2014 at 07:56 AM · raycastingonmousedown

Slow Motion Camera translate and back

Simple question I believe, but I have been here for hours looking. 1) When I click on an object in the distance (a scene w/ a billboard) & I want the camera to zoom there (slowly)

2) Now that I am close to the billboard, I want to click on another object (a woman's face on the billboard, or some text) and return to my original position & move on from there.

I am working for touch screens so it is tricky and I am not sure about RayCasting.

I have been on these boards for hours and I am going down the rabbit hole - I hope this makes sense" // First target marker to click on to go to is a game object scene (in which is Target 2 button to leave..) var target: Transform;

     //Second target marker to click on and leave
     var target2: Transform;
     
     //record the original Camera position to go back to
     var originalCamPos : Transform;
 
     // trying to contol the speed of camera movement
     // Speed in units per sec.
     var speed:float;
     //Trying to access var Step outside of the update function - not sure about this
     var step:float;
     
     
     function Update () {
         // The step size is equal to speed times frame time.
         var step = speed * Time.deltaTime;     
         }
     
     
     function OnMouseDown () {
     //record the original Camera position to return to
     originalCamPos = Camera.main.transform;
         // if I clicked on billboard to go to
         if(collider.target) {
             // Move our position a step closer to the target.
             Camera.main.transform.position = Vector3.MoveTowards(transform.position, target.position, step);
             }
     else {
         //Stay where you are or go back home...
         //Camera.main.transform.position = Vector3.MoveTowards(transform.position, (), step);
          Camera.main.transform.position = originalCamPos.transform.position;
     }
 }
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
0
Best Answer

Answer by CodeElemental · Mar 03, 2014 at 08:37 AM

The problem is, that the originalCamPos contains the actual transform reference of the camera ( hence it always returns the current position of the camera) . If you want to hold reference to the actual position, you need to extract the position only. The modified code would look something like :

 //record the original Camera position to go back to
     var originalCamPos : Vector3;
 
 function OnMouseDown () {
     
        // if I clicked on billboard to go to
        if(collider.target) {
        //record the original Camera position to return to
         originalCamPos = Camera.main.transform.position;
          // Move our position a step closer to the target.
          Camera.main.transform.position = Vector3.MoveTowards(transform.position, target.position, step);
          }
     else {
        //Stay where you are or go back home...
        //Camera.main.transform.position = Vector3.MoveTowards(transform.position, (), step);
         Camera.main.transform.position = originalCamPos;
     }


In addition , the "old" position should be "remembered" only in case of approaching the billboard (so you can go back to it later), you don't need to keep it on every MouseDown event.

Comment
Add comment · Show 2 · 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 Bentoon · Mar 03, 2014 at 06:30 PM 0
Share

Thank you Codin

still problems

  • the if(collider.target) { line is till generating an error as

    'target' is not a member of 'UnityEngine.Collider'.

I appreciate it...getting close

avatar image Bentoon · Mar 03, 2014 at 06:36 PM 0
Share

I think it's because the Target (while a variable) needs to be associated with the game Object I am applying it to (the billboard for example) and I am at a loss. Logically I understand : target = me

?

    // First target marker to click on to go to is a game object scene (in which is Target 2 button to leave..)
     var target: Transform;
     
     //Second target marker to click on and leave
     var target2: Transform;
     
 //record the original Camera position to go back to
     var originalCamPos : Vector3;
  
 
     // trying to contol the speed of camera movement
     // Speed in units per sec.
     var speed:float;
     //Trying to access var Step outside of the update function - not sure about this
     var step:float;
     
     
     function Update () {
         // The step size is equal to speed times frame time.
         var step = speed * Time.deltaTime;     
         }
     
 
 
 function On$$anonymous$$ouseDown () {
  
        // if I clicked on billboard to go to
        if(collider.target) {
        //record the original Camera position to return to
         originalCamPos = Camera.main.transform.position;
          // $$anonymous$$ove our position a step closer to the target.
          Camera.main.transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target.position, step);
          }
     else {
        //Stay where you are or go back home...
        //Camera.main.transform.position = Vector3.$$anonymous$$oveTowards(transform.position, (), step);
         Camera.main.transform.position = originalCamPos;
     }
 }
avatar image
0

Answer by Bentoon · Mar 03, 2014 at 11:48 PM

Attn Mod (I am not cross posting. Have 2 different questions)

Here is a modified solution (it doesn't work...but is quite close) The scene I am zooming into is a Game Object - It has an invisible Cube that I click on to go to the pos of the cube (trigger) and then there is another invisible collider there I would click on to go back But my mouseDown doesn't register

ZOOM IN SCRIPT:

     // I don’t seem able to pass the original Camera pos to return to. So commented out:    
     var originCamPos : Vector3;
     
     // The target marker to go to.
     var target: Transform;
     // Speed in units per sec.
     var speed:float;
     var step:float;
     
     function Update () {
         // The step size is equal to speed times frame time.
         var step = speed * Time.deltaTime;
         }
     
     
         function OnMouseDown () {
         // record original Camera position to come back to later
         originCamPos = Camera.main.transform.position;
         
         print("First");
         
         // Move our position a step closer to the target.
         Camera.main.transform.position = Vector3.MoveTowards(transform.position, target.position, step);
     }


ZOOM OUT SCRIPT:

     // The original Camera pos to return to.
     var originCamPos : Vector3;
     
     // Speed in units per sec.
     var speed:float;
     var step:float;
     
     function Update () {
         // The step size is equal to speed times frame time.
         var step = speed * Time.deltaTime;
         }
     
     function OnMouseDown () {
         print("second"); // wont register
         // Move our position a step closer to the target.
         //Camera.main.transform.position = Vector3.MoveTowards(transform.position, originPos, step);
         Camera.main.transform.position = originCamPos;
         }

Thanks for your advice

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Rigidbody and raycasting 6 Answers

2D Colliders stops working at random despite rigidbody, OnMouseDown not called 0 Answers

How to detect certain object using raycast 2 Answers

Having problems changing the material of current target. 2 Answers

Unable to use Raycasting using xbox 360 gamepad 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