Home > Software engineering >  Teleportation bug Unity 2d
Teleportation bug Unity 2d

Time:10-31

I am sorry in advance for maybe a stupid question as I am new to unity.

I have struggled with a problem that I cannot find an answer to and do not really understand. Here is the problem that I have recorded: https://youtu.be/C7Bfq--56h8

And here is my code:

SnakeMovement :

    void Start()
    {       

    }

      private void Awake()
      {
            if(!rigidbody) rigidbody = GetComponent<Rigidbody2D>();
      }


    // Update is called once per frame
     void Update ()
     {
      
     if(Input.GetKey(KeyCode.A))
     {
         angle  = Time.deltaTime * rotationSpeed;
     }
     if(Input.GetKey(KeyCode.D))
     {
         angle -= Time.deltaTime * rotationSpeed;
     }

     PositionHistory.Insert(0, transform.position);

      int index = 0;
     foreach (var body in BodyParts) {
           Vector3 point = PositionHistory[Mathf.Min(index * Gap, PositionHistory.Count - 1)];
           Vector3 moveDirection = point - body.transform.position;
           body.transform.position  = moveDirection * BodySpeed * Time.deltaTime;
            Quaternion rotation = Quaternion.LookRotation(body.transform.position - transform.position, transform.TransformDirection(Vector3.up));
            body.transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
           index  ;
     }
        
     }
     private void FixedUpdate()
      {
      rigidbody.MoveRotation(angle);
       rigidbody.velocity = rigidbody.GetRelativeVector(Vector3.right).normalized * speed;
      }


      private void GrowSnake() 
      {
            StartCoroutine(waiter());
      }

      private void OnTriggerEnter2D(Collider2D col)
     {
           if (col.gameObject.layer == 7)
            {
                  GrowSnake();
            }
     }
      IEnumerator waiter()
      {
            GameObject body = Instantiate(BodyPrefab);
            body.SetActive(false);
            BodyParts.Add(body);
            yield return new WaitForSeconds(0.2f);
            body.SetActive(true);

      }
}

}

WallTeleportation code :

private void OnTriggerEnter2D(Collider2D col) {
if (col.gameObject.layer == 6) {
    Debug.Log("GameObject1 collided with "   col.transform.position);
    col.transform.position = new Vector3(col.transform.position.x, col.transform.position.y * -1, 0);
    }
}

I am assuming that the problem arises in this part:

  int index = 0;
 foreach (var body in BodyParts) {
       Vector3 point = PositionHistory[Mathf.Min(index * Gap, PositionHistory.Count - 1)];
       Vector3 moveDirection = point - body.transform.position;
       body.transform.position  = moveDirection * BodySpeed * Time.deltaTime;
        Quaternion rotation = Quaternion.LookRotation(body.transform.position - transform.position, transform.TransformDirection(Vector3.up));
        body.transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
       index  ;
 }

I have struggled with this problem for the whole past evening and for a few hours now. Any advice would be helpful.

Thanks ahead!

P.S. English is not my native language. Sorry for the mistakes.

CodePudding user response:

When the head teleports and the body parts are trying to go to the head , the body parts are changing the direction (update the direction) of the head and so the head does colide again and teleport again. PS: English is also not my actual language.

CodePudding user response:

Teloprtation bug was caused by transforming object at the coordinates of the other portal, the other portal did the same. Thus, the head would bounce left to right continiously.

Simlly added in this line:

col.transform.position = new Vector3((col.transform.position.x * -1)   1, col.transform.position.y, 0);

1 or -1 depending on the side of the portal

  • Related