ive been trying to figure this one issue out for a while and im stuck - im making a 2d splatoon project. my plan is to have the bullet collide with an uninked tilemap, and then spawn an inked tile on a separate tilemap (so that tilemap can have ink properties). i can access the collision tilemap in the script via OnTriggerEnter2D, but i can’t access the inked tilemap within my script. Im trying to assign the tilemap in the editor using public Tilemap inkTileMap but it wont let me drag and drop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEditor.Tilemaps;
// THIS SCRIPT CONTROLS THE INK BULLET
public class Bullet : MonoBehaviour
{
public float speed;
public Rigidbody2D rb;
public GameObject inkPrefab;
Tilemap tileMap;
public Tilemap inkTileMap;
// Start is called before the first frame update
void Start()
{
rb.velocity = transform.right * speed; // makes the bullet travel forward
}
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "UnInked")
{
// variable that holds tilemap
tileMap = col.gameObject.GetComponent<Tilemap>();
Debug.Log(tileMap);
// bulletVelocity takes the bullet's velocity and changes it to either 1 or -1
// intBulletVelocity takes bulletVelocity and converts it to a Vector3Int (for tiles)
Vector3 bulletVelocity;
bulletVelocity = rb.velocity;
bulletVelocity.x = bulletVelocity.x / Mathf.Abs(bulletVelocity.x);
bulletVelocity.y = bulletVelocity.y / Mathf.Abs(bulletVelocity.y);
Vector3Int intBulletVelocity = tileMap.WorldToCell(bulletVelocity);
Debug.Log(bulletVelocity);
// destroys the tile the bullet lands on
// a bug occurs in this code: the bullet collides with the floor, but the position of the bullet is at the center
// this means the tile that is replaced is the tile where the bullet's center is. This tile is above the intended tile
// this is fixed by removing intBulletVelocity from intPosition, making the code think the bullet's center is 1 tile down
Vector3 worldPoint = transform.position;
Vector3Int intPosition = tileMap.WorldToCell(worldPoint);
intPosition.x = intPosition.x + intBulletVelocity.x;
intPosition.y = intPosition.y + intBulletVelocity.y;
tileMap.SetTile(intPosition, null);
// creates blue circle and destroys the bullet
Instantiate(inkPrefab, transform.position, transform.rotation);
Destroy(gameObject);
}
}
}
im very confused why this is happening… can anyone explain?