Hello Guys. I have a problem with this script: Code (JavaScript): #pragma strict //Declares Variables var player : GameObject; var direction : int; function Update () { //Declares local variable var playerPos = player.transform.position; //Interprets key input if(Input.GetKey(KeyCode.W)){ direction = 1; } if(Input.GetKey(KeyCode.S)){ direction = 2; } if(Input.GetKey(KeyCode.A)){ direction = 3; } if(Input.GetKey(KeyCode.D)){ direction = 4; } //What happens when a value is assigned to direction if(direction == 1){ player.transform.position = playerPos + Vector2(0,.1); } if(direction == 2){ player.transform.position = playerPos + Vector2(0,-.1); } if(direction == 3){ player.transform.position = playerPos + Vector2(-.1,0); } if(direction == 4){ player.transform.position = playerPos + Vector2(.1,0); } } I wanna make it C# script but I'm just starting to learn with Unity and dont know how to do that.. It all what I did but still not working: Code (CSharp): using UnityEngine; using System.Collections; public class SnakeCMov : MonoBehaviour { public GameObject player; public int direction; // Use this for initialization void Start (){ } // Update is called once per frame void Update () { Vector2 playerPos = new Vector2 (0, 0); player.transform.position = playerPos; if(Input.GetKey(KeyCode.W)){ direction = 1; } if(Input.GetKey(KeyCode.S)){ direction = 2; } if(Input.GetKey(KeyCode.A)){ direction = 3; } if(Input.GetKey(KeyCode.D)){ direction = 4; } //What happens when a value is assigned to direction if(direction == 1){ player.transform.position = playerPos + Vector2(0,.1); } if(direction == 2){ player.transform.position = playerPos + Vector2(0,-.1); } if(direction == 3){ player.transform.position = playerPos + Vector2(-.1,0); } if(direction == 4){ player.transform.position = playerPos + Vector2(.1,0); } } } Thanks for help.
in order to convert the js script to c# you just needed to add the class definition, add the import namespaces and change "function" to "void", and, optionally, type the variables... why did you add: Code (csharp): Vector2 playerPos = new Vector2 (0, 0); player.transform.position = playerPos; in the update function? that will reset the player position every frame (welcome to the dark side, have a cookie )
Because this line: Code (JavaScript): var playerPos = player.transform.position; was in function update in js script Oke so.. I change that: Code (JavaScript): var player : GameObject; var direction : int; to Code (CSharp): public GameObject player; public int direction; but what I have to do with this: Code (JavaScript): var playerPos = player.transform.position;