Look in walking Direction errors (C#)

I got this code for a TP game:

using UnityEngine;
using System.Collections;

public class TPWalking : MonoBehaviour {

	float h; //Intiliaze horizontal input
	float v; //Intiliaze vertical input

	Vector3 WalkDirection = new Vector3 (v,0f,h);
	// Update is called once per frame
	void Update () 
	{
		Move (h,v);
		h = Input.GetAxis("Horizontal")/10;
		v = Input.GetAxis("Vertical")/10;
	
		transform.rotation = Quaternion.LookRotation (WalkDirection);
	}

	void Move (float horizontal, float vertical)
	{
		transform.Translate (horizontal, 0, vertical);
	}
}

The walking part is working, but for the rotayion part I get this errors:

Assets/TPWalking.cs(9,46): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `TPWalking.v’

Assets/TPWalking.cs(9,51): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `TPWalking.h’

Assets/TPWalking.cs(9,53): error CS1502: The best overloaded method match for `UnityEngine.Vector3.Vector3(float, float, float)’ has some invalid arguments

Assets/TPWalking.cs(9,53): error CS1503: Argument #1' cannot convert object’ expression to type `float’

Thanks in advance!

1 Answer

1

This line:

  Vector3 WalkDirection = new Vector3 (v,0f,h);

Needs to be changed to:

   Vector3 WalkDirection;

   void Start() {  
        walkDirection = new Vector3 (v,0f,h);
   }

Yes then I get no errors,thanks! But firstly the player doesnt rotate around his own center. And second, it is hardly to explain, but if I, for example wants to walk backwards and press the "backwards key" the player turns backwards but then also walk backwards relative to him self , so he walks the wrong way. Sorry, bu I can't explain it better