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 Bentoon · Mar 08, 2014 at 05:53 AM · mouseclicktimescaleupdate function

Triggering Timescale with Mousedown

hello people (me again... but oh my I am Soooooo Close.) What's wrong with my code?

I am trying to click on an invisible rigid body that will translate my camera towards it in slow motion. Then I will do something and click on another object to return in slow motion to that first position.

I love the Zoom Effect but since I need to go to a specific point (and then back later) I don't think it will work.

Is the right way to trigger Timescale (an update function) but when I click/mouseDown/touch?

Here is what I have:

ZOOM IN SCRIPT:

     // vector3 of Camera I want to record so the Zoom out can use it too
     public var originCamPos : Vector3;
     
     // The target II will zoom to to.
     var target: Transform;
 
     
     function Update () {
                //this is Unity but I want to use Mouse Down
               if (Input.onMouseDown ("TRIGGER_In")) {
         if (Input.GetButtonDown ("Fire1")) {
             if (Time.timeScale == 1.0)
                 Time.timeScale = 0.7;
             else
                 Time.timeScale = 1.0;
             // Adjust fixed delta time according to timescale
             // The fixed delta time will now be 0.02 frames per real-time second
             Time.fixedDeltaTime = 0.02 * Time.timeScale;
         }
     }
     
     
     function OnMouseDown () {
     // record original Camera position to come back to later, but I think I am mixing Vector3 & transforms
     originCamPos = Camera.main.transform.position;    
     Camera.main.transform.position = target.transform.position;
 }
 

Should in theory work.

One problem is Semantics... Calling the if mouse down from an update script to slow time down - but then How on earth do I scale the time back up at the end?

Another question is simply Collecting the Original Cam position to return to in a Public Var

Here is The ZOOM BACK Script:

     // The original Camera pos to return to.
     public var originCamPos : Vector3;
     
         
         function Update () {
          // Again, this is Unity but I want to use Mouse Down
         // something like  : if (Input.onMouseDown ("TRIGGER_Back")) {
             if (Input.GetButtonDown ("Fire1")) {
             if (Time.timeScale == 1.0)
                 Time.timeScale = 0.7;
             else
                 Time.timeScale = 1.0;
             // Adjust fixed delta time according to timescale
             // The fixed delta time will now be 0.02 frames per real-time second
             Time.fixedDeltaTime = 0.02 * Time.timeScale;
         }
     }
     
         
     function OnMouseDown () {
         Camera.main.transform.position = originCamPos;
       }


It seems like I am close..ish am I approaching this all wrong? Thanks So much for everyone's help

~be

Comment
Add comment · Show 2
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 09, 2014 at 04:04 AM 0
Share

... Really?

No ideas?

~be

avatar image Bentoon · Mar 09, 2014 at 01:45 PM 0
Share

Thank you fifth$$anonymous$$notch!! I'm going to try Lerp now

2 Replies

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

Answer by fifthknotch · Mar 09, 2014 at 05:36 AM

This code right here is part of your problem:

 Camera.main.transform.position = target.transform.position;

This says "teleport" my camera to this location. Regardless of the timescale, your camera will instantly go to that position because timescale only affects physics and animations. Check this out from the docs:

When timeScale is set to zero the game is basically paused if all your functions are frame rate independent.

Also, timeScale affects the deltaTime so you should not have to tweak that in your code.

I would suggest instead of using timescale, use lerp. Just change the speed at which you lerp to your target. If timeScale is adding slow motion effects to other objects in your scene then use both timeScale and lerp (timeScale will affect lerp if your movement is being multiplied by time.deltaTime which would solve your problems.

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 Bentoon · Mar 09, 2014 at 10:43 PM 0
Share

Thanks This woks a bit for me.... But

1) I am only able to do this ONCE each time and no more

2) my startPos & endPos are distinct Vector 3's I must program by hand each time I use the script Since I have a FCP as the startPos of my ZoomIn script (and the endPos of my ZoomOut script) and a Plane as the endPos of my ZoomIn script (and the startPos of my ZoomOut script) can't I just be passing variables for this?

ie: how to make a thing's Transform > a Vector3?

Here is my Zoom in:

     var startPos: Vector3;
     var endPos: Vector3;
     var time : float;
   
 
     
     function On$$anonymous$$ouseDown () {
     
     var i = 0.0;
         var rate = 1.0/time;
         while (i < 1.0) {
         i += Time.deltaTime * rate;
         Camera.main.transform.position = Vector3.Lerp(startPos, endPos, i);
         yield; 
         
         }
 }


Zoom out is reverse:

         var startPos: Vector3;
         var endPos: Vector3;
         var time : float;
       
     
         
         function On$$anonymous$$ouseDown () {
         
         var i = 0.0;
             var rate = 1.0/time;
             while (i < 1.0) {
             i += Time.deltaTime * rate;
             Camera.main.transform.position = Vector3.Lerp(endPos, startPos, i);
             yield; 
             
             }
     }
 
avatar image fifthknotch · Mar 09, 2014 at 10:55 PM 0
Share

When you zoom in and then back out, is your camera located in the center of startPos and endPos or does it actually complete the movement back to the start position. I feel like since both scripts check for the mouse to be clicked they both try to move your camera at the same time. if the camera gets stuck at the midpoint between startPos and endPos, then that means the camera is lerping in both scripts.

avatar image Bentoon · Mar 10, 2014 at 02:27 AM 0
Share

Thanks so much for your help Fifth!

Each script is on a different object/Trigger. I start the zoom in from a specific Vector3 that I have put in, not necessarily where the player is, but somewhere in the vicinity, so that it Jumps to my Vector3 StarPos and then slow mo's all the way to my specific endPos. Same thing with the Zoom out, after the player scrolls around in the vicinity they jump back through a Vector3 the Vector3 startPos and then all the way back to the EndPos that I have predefined...

So I am wondering if I can get rid of the startPos entirely and only define the endPos.??

Originally I had wanted to keep track of the startPos the return the player back exactly, but I actually prefer to define it by hand in a Vector3 that I will eventually make private...

Does that make sense?

avatar image fifthknotch · Mar 11, 2014 at 05:21 PM 0
Share

Yes. First, find your player or camera by its tag or name and then use it's transform as the start point. That will get rid of the pre-set start point. I don't see anything in the scripts that you posted that would keep it from working multiple times. I will test then in a bit...

avatar image fifthknotch · Mar 11, 2014 at 06:06 PM 0
Share

I created a script that handles both zoo$$anonymous$$g in and out. Typically, I always use lerp in an Update function. This doesn't mean you can't use it in a coroutine, I just didn't in my code. The script checks for the camera to be close to the desired position and then snaps it in place to stop lerping. You start the movement by clicking the mouse button.

Also, your camera can start anywhere and it lerp from that position and return back to that position. Check out how I use the camera's current transform as its starting position:

 #pragma strict
 
 var startPos: Vector3;
 var endPos: Vector3;
 private var movePos : Vector3;
 private var moving;
 var time : float;
 
 function Start () {
     startPos = Camera.main.transform.position;
 }
 
 function Update () {
     var i = 0.0;
     var rate = 1.0/time;
     
     // Checks for input to start lerp
     if (Input.Get$$anonymous$$ouseButtonDown (0) && !moving) {
         moving = true;
     }
     
     // Lerp from current position to movePos
     // moving keeps the camera from lerping to a different position while lerping to the first position
     if (moving) {
         i += Time.deltaTime * rate;
         Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, movePos, i);
     }
     
     // movPos is set based on where the camera currently is (either startPos or endPos)
     if ( Camera.main.transform.position.z <= startPos.z+0.1 ) {
         Camera.main.transform.position = startPos;
         movePos = endPos;
         moving = false;
     }
     else if ( Camera.main.transform.position.z >= endPos.z-0.1 ) {
         Camera.main.transform.position = endPos;
         movePos = startPos;
         moving = false;
     }
 }

It's not the cleanest code, but it works. I modified your code to make it using the same variables, and I changed the mouse click part for testing purposes (I did not want to try to build an entire scene with triggers and everything). From this, you should be able to tweak your own code and figure out what the difference is between them and why yours won't work multiple times.

Show more comments
avatar image
0

Answer by Bentoon · Mar 09, 2014 at 06:55 PM

Thanks This woks a bit for me.... But

1) I am only able to do this ONCE each time and no more

2) my startPos & endPos are distinct Vector 3's I must program by hand each time I use the script Since I have a FCP as the startPos of my ZoomIn script (and the endPos of my ZoomOut script) and a Plane as the endPos of my ZoomIn script (and the startPos of my ZoomOut script) can't I just be passing variables for this?

ie: how to make a thing's Transform > a Vector3?

Here is my Zoom in:

     var startPos: Vector3;
     var endPos: Vector3;
     var time : float;
   
 
     
     function OnMouseDown () {
     
     var i = 0.0;
         var rate = 1.0/time;
         while (i < 1.0) {
         i += Time.deltaTime * rate;
         Camera.main.transform.position = Vector3.Lerp(startPos, endPos, i);
         yield; 
         
         }
 }


Zoom out is reverse:

         var startPos: Vector3;
         var endPos: Vector3;
         var time : float;
       
     
         
         function OnMouseDown () {
         
         var i = 0.0;
             var rate = 1.0/time;
             while (i < 1.0) {
             i += Time.deltaTime * rate;
             Camera.main.transform.position = Vector3.Lerp(endPos, startPos, i);
             yield; 
             
             }
     }
 
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.

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

Javascript Timescale Problem 0 Answers

Can i ignore timescale when playing AudioSource.PlayClipAtPoint ? 1 Answer

'timeScale' is not a member of 'Time' 1 Answer

Game over scene when the time is up ? 2 Answers

The Speed of Built APK 0 Answers


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