Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jul 24, 2017 at 12:48 PM by Szani for the following reason:

Problem solved by author.

avatar image
0
Question by Szani · Jul 23, 2017 at 10:01 PM · movementcoroutineaddforce

Problem with changing value in coroutine

Hi,

It's my first time here and well... it's my first game, so I'm absolute beginner. I'm trying to create Wind Trap. My player (ball), when he will be in range of wind trap should be pushed. I want my trap to "work" for 5 seconds and then let the player pass for another 10 seconds. My force changes from 0 to 10 as I want, but when I try to enter with ball (player) in range of trap (even when float force is 0) my player is still being pushed back. So here is my code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
     
     public class WindTrap : MonoBehaviour 
     {
         public float force;
         public Vector3 direction;
         public GameObject ball;
         public bool inRangeOfTrap = false;
     
         void Start () 
         {
             ball = GameObject.FindGameObjectWithTag ("Player");
             direction = transform.forward;
         }
     
         void FixedUpdate () 
         {
             StartCoroutine (ForceTimer ());
             if (inRangeOfTrap) 
             {
                 ball.GetComponent<Rigidbody> ().AddForce (direction * force);
             }
         }
     
         void OnTriggerEnter (Collider other)
         {
             if(other.gameObject.tag == "Player")
             {
                 inRangeOfTrap = true;
             }
         }
     
         void OnTriggerExit (Collider other)
         {
             if (other.gameObject.tag == "Player") 
             {
                 inRangeOfTrap = false;
             }
         }
     
         IEnumerator ForceTimer()
         {
             while (true) 
             {
                 force = 10.0f;
                 yield return new WaitForSecondsRealtime (5);
                 force = 0.0f;
                 yield return new WaitForSecondsRealtime (10);
             }
         }
     }

I will be very grateful for help - what am I missing? ;)

EDIT: Solved. I used InvokeRepeating instead of Coroutine.

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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

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

Car game and simple drift system with rigidbody.velocity 1 Answer

Hit a wall in my RTS movement controller. Issues with MoveToward and Coroutine logic. 0 Answers

2D Top-Down Character: How to make my character move using physics? 1 Answer

Move object to another position after Mouse Click with MoveTowards function 1 Answer

So this is the script and I want to be able to boost my speed with a press of a button for example "Jump",Can't increase my movement with GetbuttonDown 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