Problem solved by author.
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.
Follow this Question
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