Question by
Michaelsoft_Binbows · Jul 04, 2020 at 02:06 PM ·
2dcolliderrigidbody2dcollider2dnot working
OnCollisionEnter 2d not working
I have 2 sprites with a polygon collider 2d and a rigidbody2d, one of the two has a script for OnCollisionEnter:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class colliderScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "machine")
{
Debug.Log("Touch..");
}
}
}
But it isn't working and i think it's because of this script that let the sprite follow your mouse and snap to gird:
public void MouseFollow()
{
if (gridEnabled == true) {
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePosition.z = transform.position.z;
transform.position = Vector3.MoveTowards(transform.position, mousePosition, moveSpeed * Time.deltaTime);
float snapInverse = 1 / snapValue;
float x, y, z;
x = Mathf.Round(transform.position.x * snapInverse) / snapInverse;
y = Mathf.Round(transform.position.y * snapInverse) / snapInverse;
z = depth; // depth from camera
transform.position = new Vector3(x, y, z);
}
if (Input.GetKeyDown(KeyCode.R))
{
gridEnabled = false;
}
}
this one is on both of the sprites that need to collide with eachother. Here you see a picture of the Rigidbody2d and the polygoncollider2D:
These two peaces need to collide:
thank you in advance!
aantekening-2020-07-04-161116.jpg
(27.8 kB)
aantekening-2020-07-04-151745.jpg
(43.3 kB)
Comment