- Home /
 
               Question by 
               zymlok · Sep 06, 2017 at 06:00 PM · 
                scripting probleminstantiatescripting beginnermovement scriptgrid based game  
              
 
              Auto-move on grid and draw line problem
Hi, i write a grid-based game with automove player (something like snake but line after player is always drawing) I have a problem when i change direction, sometimes te line is not drawing. Can anyone help me, what I doing wrong?
There is my code: public GameObject line;
     Vector3 lastWall;
     Vector3 pos;
     Vector3 linePos;
 
     public float speed = 0.1f;
     int direction;
     int lastDirection;
 
     Transform tr;
 
     void Start () {
         // Initial Velocity
         GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
 
         pos = transform.position; // Take the current position
         tr = transform;
 
         direction = 1;
 
     }
     void Update()
     {
 
         if (Input.GetKey(leftKey)) {direction = 1;}
         if (Input.GetKey(rightKey))    {direction = 2;}
         if (Input.GetKey(upKey)) {direction = 3;}
         if (Input.GetKey(downKey)) {direction = 4;}
 
         if (direction == 1 && tr.position == pos) {pos += Vector3.left;}
         if (direction == 2 && tr.position == pos) {pos += Vector3.right;}
         if (direction == 3 && tr.position == pos) {pos += Vector3.up;}
         if (direction == 4 && tr.position == pos) {pos += Vector3.down;}
 
     
         transform.position = Vector3.MoveTowards (transform.position, pos, speed); 
                 spawnWall ();
     }
 
     void spawnWall() {
 
         linePos = pos;
 
             if (direction == 2) {linePos += Vector3.left;}
             if (direction == 1) {linePos += Vector3.right;}
             if (direction == 4) {linePos += Vector3.up;}
             if (direction == 3) {linePos += Vector3.down;}
 
 
         if (lastWall != linePos && lastDirection == direction) {
             Instantiate(line, linePos, Quaternion.identity);
         }
             
         lastDirection = direction;
         lastWall = linePos;
 
     }
and problem is:  and sometimes is good, bud sometimes the wall ist'n instate...
 and sometimes is good, bud sometimes the wall ist'n instate...
 
                 
                unity.jpg 
                (85.1 kB) 
               
 
              
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                