Search Unity

2D Snake Head Moving Script

Discussion in 'Scripting' started by Dexydon, Dec 20, 2015.

  1. Dexydon

    Dexydon

    Joined:
    Dec 20, 2015
    Posts:
    10
    Hello Guys.
    I have a problem with this script:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. //Declares Variables
    4. var player : GameObject;
    5. var direction : int;
    6.  
    7. function Update () {
    8. //Declares local variable
    9. var playerPos = player.transform.position;
    10.  
    11. //Interprets key input
    12. if(Input.GetKey(KeyCode.W)){
    13. direction = 1;
    14. }
    15. if(Input.GetKey(KeyCode.S)){
    16. direction = 2;
    17. }
    18. if(Input.GetKey(KeyCode.A)){
    19. direction = 3;
    20. }
    21. if(Input.GetKey(KeyCode.D)){
    22. direction = 4;
    23. }
    24.  
    25. //What happens when a value is assigned to direction
    26. if(direction == 1){
    27. player.transform.position = playerPos + Vector2(0,.1);
    28. }
    29. if(direction == 2){
    30. player.transform.position = playerPos + Vector2(0,-.1);
    31. }
    32. if(direction == 3){
    33. player.transform.position = playerPos + Vector2(-.1,0);
    34. }
    35. if(direction == 4){
    36. player.transform.position = playerPos + Vector2(.1,0);
    37. }
    38. }
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SnakeCMov : MonoBehaviour {
    5.     public GameObject player;
    6.     public int direction;
    7.  
    8.  
    9.     // Use this for initialization
    10.     void Start (){
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         Vector2 playerPos = new Vector2 (0, 0);
    16.         player.transform.position = playerPos;
    17.         if(Input.GetKey(KeyCode.W)){
    18.             direction = 1;
    19.         }
    20.         if(Input.GetKey(KeyCode.S)){
    21.             direction = 2;
    22.         }
    23.         if(Input.GetKey(KeyCode.A)){
    24.             direction = 3;
    25.         }
    26.         if(Input.GetKey(KeyCode.D)){
    27.             direction = 4;
    28.         }
    29.         //What happens when a value is assigned to direction
    30.         if(direction == 1){
    31.             player.transform.position = playerPos + Vector2(0,.1);
    32.         }
    33.         if(direction == 2){
    34.             player.transform.position = playerPos + Vector2(0,-.1);
    35.         }
    36.         if(direction == 3){
    37.             player.transform.position = playerPos + Vector2(-.1,0);
    38.         }
    39.         if(direction == 4){
    40.             player.transform.position = playerPos + Vector2(.1,0);
    41.         }
    42.    
    43.     }
    44. }
    Thanks for help.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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):
    1.  
    2. Vector2 playerPos = new Vector2 (0, 0);
    3. player.transform.position = playerPos;
    4.  
    in the update function? that will reset the player position every frame




    (welcome to the dark side, have a cookie :D)
     
  3. Dexydon

    Dexydon

    Joined:
    Dec 20, 2015
    Posts:
    10
    Because this line:
    Code (JavaScript):
    1. var playerPos = player.transform.position;
    was in function update in js script
    Oke so..
    I change that:
    Code (JavaScript):
    1. var player : GameObject;
    2. var direction : int;
    to
    Code (CSharp):
    1.  public GameObject player;
    2. public int direction;
    but what I have to do with this:
    Code (JavaScript):
    1. var playerPos = player.transform.position;