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 crasyboy42 · Nov 07, 2010 at 03:56 PM · script-ready-to-use

script act weird

Hallo my script changes a variable back to true but it says it goes to false.

i need help it is a fishing script but i can't found the problem.

here is simple explanation what i must do :

  1. see if the player is in the cube(with isTrigger on)
  2. if he pres e go and fish
  3. if he is fishing generate random waits (for every tug)
  4. after al those tugs reset the variables

after 3 it goes the wrong way please help me

here is the code

var target : Transform; var FishingPool : Transform; var rotationY :float= 0.0;

private var showGUI = false; private var isFishing = false; private var tugFish = false; private var changeble = true;

function Update () { print("show gui:"+showGUI +" isFishing:"+ isFishing +" tugFish:" + tugFish +" changeble :"+ changeble); if(showGUI) { if(Input.GetKeyDown("e")) { if(!isFishing) { target.GetComponent(ThirdPersonController).enabled = false; target.localEulerAngles.y = rotationY; target.animation.Stop(); target.animation.Play("fishing"); isFishing = true; } else { target.GetComponent(ThirdPersonController).enabled = true; isFishing = false; } } if(!isFishing) target.GetComponent(ThirdPersonController).enabled = true; else fish(); } if(changeble) tugFish = false; }

function OnTriggerEnter() { showGUI = true; }

function OnTriggerExit() { showGUI = false; }

function OnGUI() { if(showGUI) { if(!isFishing) GUI.Label(Rect(Screen.width/2-50, 50,100,30),"Press E to Fish"); else GUI.Label(Rect(Screen.width/2-75, 50,150,30), "Press E to cancel fishing");

     if(isFishing) {
         if(tugFish && GUI.Button(Rect(Screen.width/2 +50,50,60,30),
                                  "PULL!")) {
         }
         else if(GUI.Button(Rect(Screen.width/2 +50,50,60,30),"PULL!")) {
             tugFish = false;
             isFishing = false;
             changeble = true;
         }
     }
 }

}

function restart() { yield WaitForSeconds(2); target.GetComponent(ThirdPersonController).enabled = true; }

function fish() { if(isFishing) { changeble = false; var tug1 = Random.Range(7, 12); var tug2 = Random.Range(6, 10); var tug3 = Random.Range(1, 2);

     yield WaitForSeconds(tug1);
     target.animation.Play("tug");

     yield WaitForSeconds(tug2);
     target.animation.Play("tug");
     tugFish = true;

     yield WaitForSeconds(tug3);
     changeble = true;

     yield;
     isFishing = false;
     tugFish = false;
 }

}

Comment
Add comment · Show 1
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 Loius · Nov 08, 2010 at 04:39 PM 0
Share

"it goes the wrong way"? What goes the wrong way, and what's the right way?

1 Reply

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

Answer by skovacs1 · Nov 08, 2010 at 07:23 PM

There are several things to consider that are troublesome with your script. In my answers, I'll spell English words correctly, even if your variables are not named in proper English words, so please correct for your usage wherever appropriate:

OnTrigger...

  1. OnTriggerEnter and OnTriggerExit take a Collider. In order for your functions to override the built-in functions, you need to define functions with the same signatures and therefore taking in Colliders function OnTriggerEnter(other : Collider) and function OnTriggerExit(other : Collider).
  2. Your OnTrigger...s do not check the object. This means that when anything enters or exits, it will change your boolean.
  3. As per the documentation on OnTrigger... functions, one of the two objects will need a RigidBody or CharacterController.

Tracing your code paths

State:

First run:

  • showGUI is false.
  • changeable is true.
  • Every frame, Update sets tugFish to false.

OnTriggerEnter:

  • showGUI is true.
  • isFishing is false.
  • changeable is true.
  • Every GUI update, OnGUI will show a label "Press E to Fish".
  • Every frame, Update, target's thirdPersonController component is found and enabled and tugFish is set to false.

OnTriggerEnter, then OnTriggerExit:

  • showGUI is false.
  • isFishing is false.
  • changeable is true.
  • target's thirdPersonController component is still enabled.
  • Every frame, tugFish is set to false.

OnTriggerEnter, then key "e" is pressed down:

  • showGUI is true.
  • isFishing is false.
  • changeable is true.
  • The first frame that e is pressed down, Update finds target's thirdPersonController component and disables it, stuff happens and isFishing is set to true and then fish is called.
  • fish sets changeable to false and then yields.
  • Every GUI update after this OnGUI will show a label "Press E to cancel fishing" and a button which will set is fishing to false, tugFish to false(which it already is or you wouldn't have the button) and changeable to true.

OnTriggerEnter after "e" is pressed down:

  • showGUI is true.
  • isFishing is true.
  • changeable is false.
  • Every frame Update calls fish().
  • Every GUI update, OnGUI will show a label "Press E to cancel fishing" and a button which will set is fishing to false, tugFish to false(which it already is or you wouldn't have the button) and changeable to true.
  • fish will yield twice, after which tugFish will be true and fish will yield. OnGUI will now show a button that doesn't do anything.
  • fish will set changeable to true and yield again. Update will set tugFish to false.
  • fish will set isFishing to false and tugFish to false (which it already is).
  • Every GUI update, OnGUI will show a label "Press E to Fish".
  • Update the next frame will not call fish, but will find target's thirdPersonController component and disable it.

If anything should ever exit your trigger during any of these intermediate states, the script will essentially fall apart. fish will be left at the last yield that was encountered and if ever called after this will resume from there. fishing may still be true, changeable may still be false, tugFish may still be true and the thirdPersonController would still be disabled and the GUI would show nothing to let them know what has happened.

Your logic is a mess. You set variables to values when they aren't even changing and you depend on a certain setup but then don't properly reset it when you're done. You use yield statements in a function which really should be in a coroutine.

Here's something more sensible:

var target : Transform; var rotationY : float= 0.0;

private var inside : boolean = false; private var fishing : boolean = false; private var fishOnHook : boolean = false;

function OnTriggerEnter(other : Collider) { //Check what it is inside = true; }

function OnTriggerExit(other : Collider) { //Check what it is StopFishing(); inside = false; }

function OnGUI() { if(inside) { if(!fishing) GUI.Label(Rect(Screen.width/2-50, 50,100,30),"Press E to Fish"); else { GUI.Label(Rect(Screen.width/2-75, 50,150,30), "Press E to cancel fishing"); if(GUI.Button(Rect(Screen.width/2 +50,50,60,30),"PULL!")) { if(fishOnHook) { //do something } StopFishing(); } } } }

function Update () { if(inside && Input.GetKeyDown("e")) { if(!fishing) StartCoroutine("Fishing"); else StopFishing(); } }

function StopFishing() { StopCoroutine("Fishing"); target.GetComponent(ThirdPersonController).enabled = true; target.animation.Stop(); fishOnHook = false; fishing = false; }

function Fishing() { fishing = true; target.GetComponent(ThirdPersonController).enabled = false; target.localEulerAngles.y = rotationY; target.animation.Stop(); target.animation.Play("fishing");

 yield WaitForSeconds(Random.Range(7, 12));
 target.animation.Play("tug");

 yield WaitForSeconds(Random.Range(6, 10));
 target.animation.Play("tug");
 fishOnHook = true;

 yield WaitForSeconds(Random.Range(1, 2));
 fishOnHook = false;
 target.GetComponent(ThirdPersonController).enabled = true;
 fishing = false;

}

Comment
Add comment · Show 3 · 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 crasyboy42 · Nov 09, 2010 at 06:57 AM 0
Share

thanks i try to understand what i did wrong i think i messt up the trigger Enter and Exit and i run the function fish to much right?

i am only 14 but i try to understand everything :)

and thanks for helping me i was about 3 houres bussy to fix my problem :)

avatar image crasyboy42 · Nov 09, 2010 at 07:12 AM 0
Share

i have 1 question how can i make a bar like if he waits for the first time the point int he bar is in the green etc and he is rising towards the red(then you can catch the fish)?

avatar image skovacs1 · Nov 09, 2010 at 03:20 PM 0
Share

Progress bars can be done any number of ways, just search 'progress bar' (http://answers.unity3d.com/search?q=progress+bar) and you'll find a few. Personally, I like the shader approach given here (http://www.unifycommunity.com/wiki/index.php?title=Reveal_Non-Uniform_Texture).

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

Sniper zoom when right click? 1 Answer

Unity freezes when I play or run 1 Answer

Resouce script help 3 Answers

My project isn't saving. 1 Answer

How do I create a new object in the Editor as a child of another object? 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