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 pretender · Aug 06, 2010 at 03:05 PM · lerpcomponentzoom

using yield in components

hello, i was wondering does someone can help me with the code i am using. i am using this code in a lot of places in my game:

function ZoomIn(distance : float,endDistance : float){

var t = 0.0; while (t<1.0){

 t+=Time.deltaTime *(1.0/0.2);       
 cam.distance=Mathf.Lerp(distance,endDistance,t);
 yield;

}

}

the question is how to structure the code, so that i can make this a separate script and use it as a component? is there a way to use it like this:

cam.distance=ZoomComponent.ZoomIn(cam.distance,endDistance);

ZoomComponent being the reference to the script where ZoomIn function is. sorting this out would be from great help!

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 Eric5h5 · Aug 06, 2010 at 04:16 PM

Since ZoomIn is a coroutine, you can't return a value that can be assigned to cam.distance. A way around this is to use a reference value, so the value you pass in is modified inside the coroutine. This is a little tricky, since while you can use reference values in Javascript, you can't declare them, and even in C#, you can't declare reference values directly when using IEnumerators (coroutines). A way around that is to use a custom class, since classes are always reference values:

function ZoomIn (refFloat : RefFloat, start : float, end : float, speed : float) {

 var t = 0.0;
 while (t &lt; 1.0) {

     t += Time.deltaTime * speed;       
     refFloat.a = Mathf.Lerp(start, end, t);
     yield;

 }

}

class RefFloat { var a : float;

 function RefFloat (a : float) {
     this.a = a;
 }

}

You can use it like this:

function Start () { var foo = new RefFloat(0.0); var zoomScript : Zoom = GetComponent(Zoom); zoomScript.ZoomIn(foo, 0.0, 1.0, .5);

 while (true) {
     Debug.Log (foo.a);
     yield;
 }

}

So you can see that the "foo" variable is changed by the function. In your case, the "distance" variable would have to be changed from a float to a RefFloat, and you would access it by using "cam.distance.a" instead of "cam.distance".

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
avatar image
1
Best Answer

Answer by Jens T · Aug 06, 2010 at 03:51 PM

Your ZoomIn function does not return a value, so you cannot do this.

What i suppose you are modifying is the camera position, so all you ZoomComponents need to be able to access the camera. I think the best way is to pass the world X,Y,Z values of your object (or better yet, the Transform of the GameObject you are zooming on) and then have the zoomComponent modify the the camera position. The reference to the camera should be made by creating a variable like this in the zoomComponent.

var camera:GameObject

and then dragging the MainCamera onto this scriptComponent in the inspector. (You can also get it by camera = GameObject.Find("MainCamera").

Take care however, that there is not multiple zoomComponents affecting the single camera simoultainously. You can solve this by static variables.

I am quite new to Unity, so dont take me for a pro. Hope I can be of help.

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

No one has followed this question yet.

Related Questions

How to zoom in at a good pace? 1 Answer

Changing camera position to shoot through scope on gun 1 Answer

Sniper Scope? 1 Answer

How do I make my script switch back to my regular animations 1 Answer

How to zoom in smoothly with orthographic camera? 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