Im begining in Unity, so I might be missing something, but i have 4 game objects: 2 players in each side of the screen, and 2 virus on the midlle. The players can shoot boubles to move the virus away from them. To do that i have this code in each virus:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class greenVirus : MonoBehaviour
{
[SerializeField] Transform otherVirus;
Vector3 horizontal_move = new Vector3(1.4f, 0);
Vector3 vertical_move = new Vector3(0, 3);
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "blueBouble(Clone)"
&& transform.position.x < 5.6f
&& otherVirus.position != transform.position + horizontal_move)
{
transform.position += horizontal_move;
Destroy(collision.gameObject);
}
else if (collision.gameObject.name == "redBouble(Clone)"
&& transform.position.x > -5.6f
&& otherVirus.position != transform.position - horizontal_move)
{
transform.position += -horizontal_move;
Destroy(collision.gameObject);
}
else
{
Destroy(collision.gameObject);
}
}
}
The viruses shoul move ins steps of 1.4 units, to a maximum of 5.6/-5.6.
But when i try to run this, everyting goes fine except for when i try to “centralize” the virus again with the boubles (making the x value 0). In the game view and the scene view I can clearly see the the virus is in the correct place, but in the inspector the x position shows as -2.384186e-07, which is really strange. I have other function to move the virus around, but they seem to be working properly, the only probem i find is here.
Hope someone can help me, and thenks in advance