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 ().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? ;)
↧