So I’m trying to get a whip to work as a grappling hook as well, I am going with a basic to the right moving command for now but I want to be able to make it not instantaneous, but I can’t get a wait function to work, here is my code:
using UnityEngine;
using System.Collections;
public class WhippingMotionLeft : MonoBehaviour
{
private int timer = 0;
private bool collided = false;
private float whip;
// Use this for initialization
void Start ()
{
}
IEnumerator Wait()
{
print ("Wait");
yield return new WaitForSeconds(1);
}
void OnTriggerEnter2D (Collider2D col)
{
if (col.gameObject.tag != "Player" && col.gameObject.tag != "Bullet" && col.gameObject.tag != "Handle") {
// Damage the thing I hit
Enemy e = col.gameObject.GetComponent<Enemy> ();
if (e != null)
e.Damage ();
timer = 5;
collided = true;
}
else if (col.gameObject.tag == "Player")
{
Destroy (gameObject);
}
else if (col.gameObject.tag == "Handle")
{
for(int i = 0; i < 5; i++)
{
StartCoroutine(Wait());
Vector3 player = GameObject.Find ("hero").transform.position;
player.Set(player.x - 1f, player.y, player.z);
GameObject.Find ("hero").transform.position = player;
}
Destroy(gameObject);
}
}
// Update is called once per frame
void Update ()
{
whip = GameObject.Find ("whip").transform.position.y;
Vector3 temp = transform.position;
temp.y = whip;
transform.position = temp;
if (timer == 5 && collided == false)
{
rigidbody2D.velocity = new Vector2 (25f, 0);
timer = 0;
}
else if (timer == 5 && collided == true)
{
rigidbody2D.velocity = new Vector2 (-25f, 0);
}
if (timer == 15)
{
Destroy(gameObject);
}
timer++;
}
}