MovePlayer() Coroutine C#

Hi,

For some reason my coroutine is not working. I want to be able to access it over different scripts. Here is the full PlayerMovement.cs :

`using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
// Change in Position Varaibles
private static Vector3 lastPosition;

// Values are set in Unity3D Interface
public static float MoveSpeed = 40,
RotateSpeed = 200
;

public static float MoveForward,
MoveRotate
;
public static IEnumerator Move_Player()
{
MoveForward = Input.GetAxis(“Vertical”) * MoveSpeed * Time.deltaTime;
MoveRotate = Input.GetAxis(“Horizontal”) * RotateSpeed * Time.deltaTime;

// Move the player
transform.Translate(Vector3.forward * MoveForward);
transform.Rotate(Vector3.up * MoveRotate);

if (Vector3.Distance(lastPosition, transform.position) > 0.01f)
{
    lastPosition = transform.position;
    StartCoroutine(PlayerParticles.DestroyParticles());
}

yield return 0;

}
`

}

I am getting several errors regarding non-static fields, methods or properties, the object 'transform' does not contain a defintion for 'position' and so on. This is strange because the code works as it already is but only within the script that the method (Move_Player) is in. I can see that the coroutines need something more..

Well first off I'll ask why exactly is everything in your class marked static, anyway? I am assuming this script is attached to your player, so in that case all of these static methods/properties are unnecessary.

I believe that is a large source of your problem there.

Secondly, your coroutine, as it is currently written, will only work for 1 frame, but I'll explain that later on.

You asked what a coroutine is for in your comment. Well, it has many uses, really. But what I like to think of it as is a loop that you have control over where it pauses until the next frame, where it picks up upon reaching the frame and how long it loops for. This makes the coroutine the prime solution for scripting little events that would otherwise be a tangled mess of spaghetti code if they were nested inside of, let's say, Update().

There are more uses than that, like breaking up procedures in AI scripts across multiple frames to reduce processing on a per-frame basis. And some other really cool stuff that I won't get into here.

So, to fix your problem with your coroutine is pretty simple, once you understand how they work:

public IEnumerator Move_Player()
{
  while(true)
  {
    MoveForward = Input.GetAxis("Vertical")  * MoveSpeed * Time.deltaTime;
    MoveRotate = Input.GetAxis("Horizontal") * RotateSpeed * Time.deltaTime;

    // Move the player
    transform.Translate(Vector3.forward * MoveForward);
    transform.Rotate(Vector3.up * MoveRotate);

    if (Vector3.Distance(lastPosition, transform.position) > 0.01f)
    {
      lastPosition = transform.position;
      StartCoroutine(PlayerParticles.DestroyParticles()); //this might not be safe performance-wise. just an fyi.
    }

    yield return 0;
  }
}

This will keep your coroutine running forever, so you'll want to adapt that to suit your needs. You're also going to need to start your coroutine from somewhere - perhaps in Start():

void Start()
{
  StartCoroutine(Move_Player());
}

I have a tutorial on coroutines here for more info.

Lastly, I think with what you need - especially for a player controller script, I wouldn't even suggest using Coroutines. Instead, why not use Unity's built in Update() method which is called every frame?

Hope that helps.

==

http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html