Struggling with Coroutines...

Hi guys,

Im having some problem with the script of my game. I have to make some platform to move in the plane X,Y continually and I dont know hot to actually do it. The game is a platform project in which you are a ball that’s always bouncing between these platforms, some platforms have different physics effects on the ball like increasing the X,Y velocity while bouncing, there are other platform that’s destroyed when the ball hits it twice…

EDIT: Im coding in C# and this is what I’ve got so far.
BallControll Script

using System.Collections;

public class BallControl : MonoBehaviour {

    public float velocity = 2;

    bool alreadyBounced;
    bool boost;
    float boostMultiplier;
    Vector3 boostVelocityAdd;

    // Update is called once per frame
    void Update ()
    {
        alreadyBounced = false;
        float appliedVelocity = velocity * (boost ? boostMultiplier : 1);
        Vector3 direction = Vector3.zero;

        if(Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
        {
            direction = Vector3.left; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange);
        }
        if(Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
        {
            direction = Vector3.right; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange);
        }
        if(Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
        {
            direction = Vector3.forward; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange);
        }
        if(Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
        {
            direction = -Vector3.forward; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange);
        }
        if(Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.A))
        {
            direction = -Vector3.left; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange);
        }
        if(Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.D))
        {
            direction = -Vector3.right; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange);
        }
        if(Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKeyUp(KeyCode.W))
        {
            direction = -Vector3.forward; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange);
        }
        if(Input.GetKeyUp(KeyCode.DownArrow) || Input.GetKeyUp(KeyCode.S))
        {
            direction = Vector3.forward; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange);
        }
    }

    public void Bounce(float upVelocity)
    {
        if(!alreadyBounced)
        {
            Debug.Log("Bounce");
            alreadyBounced = true;
            float downVelocity = rigidbody.velocity.y;
            rigidbody.AddForce(Vector3.up * (-downVelocity + upVelocity), ForceMode.VelocityChange);
            ResetBoost();
        }
    }

    public void Boost(float multiplier){
        if(!boost){
            Debug.Log("Boost");
            StartCoroutine(BoostCoroutine(multiplier));
        }
    }

    public void ResetBoost(){
        if(boost){
            Debug.Log("Reset boost");
            boost = false;
            Vector3 velocityAdd = rigidbody.velocity;
            velocityAdd.x = velocityAdd.x / boostMultiplier * (boostMultiplier - 1);
            velocityAdd.z = velocityAdd.z / boostMultiplier * (boostMultiplier - 1);
            velocityAdd.y = 0;
            rigidbody.AddForce(-velocityAdd, ForceMode.VelocityChange);
        }
    }



    IEnumerator BoostCoroutine(float multiplier){
        yield return 0;
        yield return new WaitForFixedUpdate();
        boost = true;
        boostMultiplier = multiplier;
        Vector3 velocityAdd = rigidbody.velocity;
        Debug.Log(velocityAdd);
        velocityAdd.x = velocityAdd.x * (boostMultiplier - 1);
        velocityAdd.z = velocityAdd.z * (boostMultiplier - 1);
        velocityAdd.y = 0;
        rigidbody.AddForce(velocityAdd, ForceMode.VelocityChange);
        Debug.Log(velocityAdd);
    }

}

This is the Platforms Triggers Script

using UnityEngine;
using System.Collections;

public class BoostTrigger : BounceTrigger {

    public float boostMultiplier = 2;

    public override void OnTriggerEnter (Collider collider)
    {
        base.OnTriggerEnter (collider);
        collider.GetComponent<BallControl>().Boost(boostMultiplier);
    }

}

Actually the others platforms heritage from this Script and overrides the OnTriggerEnter with the actual code of the platform. The moving platform I’m asking about should move betwen two static points continuisly and still have the bounce effects like the others do.

Thank you.

It would be useful to see what you already have. Don’t forget to use code tags:

1 Like

What exactly happens now and what exactly would you like to achieve?