- Home /
Convert vector2int to vector3
Hi guys,
I have the following situation in my game:
I have a matrix 6x6 and I store the player's position in a vector2Int, as you can see in the image.
And when he is hit by a bullet, I'd to instantiate on his left, right, behind and front a gameobject. However I can't use a vector2int to do it, I've already tryed to do this, but I got an error message:
private void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Player2")
{
player.Health += Healing;
Instantiate(Flame, player.GridPosition.y+1, Quaternion.identity);
Instantiate(Flame, player.GridPosition.y -1, Quaternion.identity);
Instantiate(Flame, player.GridPosition.x + 1, Quaternion.identity);
Instantiate(Flame, player.GridPosition.x - 1, Quaternion.identity);
Destroy(this.gameObject);
}
}
Is there a way that I could convert this vector2int into a vector3? How could I do it?
You're getting an error message because player.GridPosition.y+1
will return an integer and instantiate uses a Vector3 for that argument.
I don't know the orientation of your board, but assu$$anonymous$$g that it's on the XZ plane, but you can start by seeing where things end up with this and going from there:
Vector3 coordXZ = new Vector3 (player.GridPosition.x, 1, player.GridPosition.y);
Instantiate (Flame, coordXZ + Vector3.forward, Quaternion.identity);
Instantiate (Flame, coordXZ - Vector3.forward, Quaternion.identity);
Instantiate (Flame, coordXZ + Vector3.right, Quaternion.identity);
Instantiate (Flame, coordXZ - Vector3.right, Quaternion.identity);
I don't think it's the case, but if that's aligned with the XY-plane then start with:
Vector3 coordXY = new Vector3 (player.GridPosition.x, 1, player.GridPosition.y);
Instantiate (Flame, coordXY + Vector3.up, Quaternion.identity);
Instantiate (Flame, coordXY - Vector3.up, Quaternion.identity);
Instantiate (Flame, coordXY + Vector3.right, Quaternion.identity);
Instantiate (Flame, coordXY - Vector3.right, Quaternion.identity);
Answer by HenriMR · Nov 19, 2019 at 03:24 PM
I think the problem isn't that.
In this line:
Instantiate(Flame, player.GridPosition.y+1, Quaternion.identity);
player.GridPosition.y+1 is not a Vector2Int, it is just a int.
Try this:
Instantiate(Flame, player.GridPosition+ new Vector2Int(0,1), Quaternion.identity);
Instantiate(Flame, player.GridPosition + new Vector2Int(0,-1), Quaternion.identity);
Instantiate(Flame, player.GridPosition + new Vector2Int(1,0), Quaternion.identity);
Instantiate(Flame, player.GridPosition + new Vector2Int(-1,0), Quaternion.identity);
What is the Error Message?