Lidia
1
Hello, I need help with a simple script. I am a super noob and I am trying to write a script to make the player move by fixed steps (like in a grid) using c# and unity 4.3. I tried (following a wiki example) to use a coroutine because I want to stop receiving input from the keyboard until the movement to the next position in the grid is done, but I keep getting compiler errors. Here is what I’ve done so far:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public GameObject player;
public float speed;
private Vector3 p;
void Start ()
{
// player position
p = player.transform.position;
//starts coroutine CoUpdate()
StartCoroutine(CoUpdate());
}
//coroutine
IEnumerator CoUpdate()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
yield return move(Vector3.right);
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
yield return move(Vector3e.left);
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
yield return move(Vector3.forward);
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
yield return move(Vector3.back);
}
else
{
yield return null;
}
}
void move(Vector3 direction)
{
transform.Translate(direction * speed * Time.deltaTime, Space.Self);
}
}
With this code compiler complains that "cannot convert type void to object’, and even if I add
new after yield it throws errors. How can I fix it?
Anything to be yielded (here you are yielding the result of a call to move) should be an IEnumerator.
What you probably want to do is:
if(Input.GetKeyDown(KeyCode.RightArrow))
{
move(Vector3.right);
yield return null;
}
// The rest of cases
// ...