2
votes

I need to make my character wall run, but I have code problems with IEnumerator

This is for Unity 4.5.x, code written on C#

using UnityEngine;
using System.Collections;

public class Moving : MonoBehaviour {

public float speed = 6.0F;
public float jumpSpeed = 8.0F; 
public float gravity = 20.0F;
public float runTime = 1.0f;
private Vector3 moveDirection = Vector3.zero;
private bool isWallL = false;
private bool isWallR = false;
private RaycastHit hitL;
private RaycastHit hitR;
private int jumpCount = 1;

IEnumerator afterRun() {
    yield return new WaitForSeconds (runTime);
    isWallL = false;
    isWallR = false;
    gravity = 20;
}
void Update() {
    CharacterController controller = GetComponent<CharacterController>();

    if (controller.isGrounded) {
        jumpCount = 0;
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;

        if (Input.GetButton("Jump"))
            moveDirection.y = jumpSpeed;
    }

    if (Input.GetKeyDown (KeyCode.Space) && !controller.isGrounded && jumpCount <= 1) {
        if (Physics.Raycast (transform.position, -transform.right, out hitL, 1)){
            if (hitL.transform.tag == "Wall"){
                isWallL = true;
                isWallR = false;
                jumpCount = 1;
                gravity = 0;
                StartCoroutine (afterRun);
            }
        }
        if (Physics.Raycast (transform.position, transform.right, out hitR, 1)){
            if (hitR.transform.tag == "Wall"){
                isWallL = false;
                isWallR = true;
                jumpCount = 1;
                gravity = 0;
                StartCoroutine (afterRun);
            }
        }
    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
    }
}

Expected no error, but I have two of:

error CS1502: The best overloaded method match for UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator)' has some invalid arguments" and "error CS1503: Argument #1' cannot convert method group' expression to type System.Collections.IEnumerator'.

3
CharacterController controller = GetComponent<CharacterController>(); no need to call this in update you can just call it once in Start method :) - phantasm
@phantasm actually not you can but rather you should! ;) GetComponent is very performance intense - derHugo
afterRun is a function not a IEnumerator , So sould use StartCoroutine(afterRun()) - TimChang

3 Answers

2
votes

afterRun in your code is a function, but you call it without brackets. So:

StartCoroutine (afterRun());

for example:

namespace someNamespace
{ 
    public class SomeClass
    {
        IEnumerator afterRun()
        {
            yield return new WaitForSeconds(3);            
        }

        public void Test(IEnumerator enumerator)
        {
            while(enumerator.MoveNext())
            {
                //do some work
            }
        }

        public void YoureCode()
        {
            Test(afterRun());
        }
    }

    public class WaitForSeconds
    {
        public WaitForSeconds(int a)
        {            
        }
    }
}

For more information see enter link description here

1
votes

Following the documentation for Unity coroutines , it appears that the coroutine function must be called as StartCoroutine ("afterRun");

1
votes

Why not like this:

private IEnumerator coroutine;

and then set it and call it:

coroutine = afterRun();
StartCoroutine(coroutine);