- Home /
Cutting mechanic not interacting with my game objects.
Hey everyone, so I'm trying to make a fruit ninja game, not the biggest thing in the world, but, for the cutting mechanic, something doesn't work. I do have an idea of what it might be but i still need some help.
Basically, my objects spawn randomly on the screen, but all of them have the Z coordinate at 500, this is because for some reason when they have the Z at 1 just like the canvas and the image i have on my canvas, they objects spawn behind the imagine. I do think the cutting object also spawns behind the canvas image, because it won't show up on screen but it does spawn because it appears on the hierarchy, but it can't interact with the other objects, which is my problem.
I"ll post screenshots with the objects, canvas, the canvas image and the cut object just to show it, maybe i can make some changes to them so they can interract better? Idk. This is the code for my cutting:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sword : MonoBehaviour
{
public GameObject CutPrefab;
public float cutLifeTime;
private bool dragging;
private Vector2 SwipeStart;
private Vector2 SwipeEnd;
void Update()
{
if(Input.GetMouseButtonDown(0))
{
dragging = true;
SwipeStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
else if (Input.GetMouseButtonUp(0) && dragging == true )
{
dragging = false;
SpawnCut();
}
}
private void SpawnCut()
{
SwipeEnd = Camera.main.ScreenToWorldPoint(Input.mousePosition);
GameObject cutInstance = Instantiate(CutPrefab,SwipeStart, Quaternion.identity);
cutInstance.GetComponent<LineRenderer>().SetPosition(0,SwipeStart);
cutInstance.GetComponent<LineRenderer>().SetPosition(1,SwipeEnd);
Vector2[] colliderPoints = new Vector2[2];
colliderPoints [0] = Vector2.zero;
colliderPoints [1] = SwipeEnd - SwipeStart;
cutInstance.GetComponent<EdgeCollider2D>().points=colliderPoints;
Destroy(cutInstance,cutLifeTime);
}
}
And these are the images: Canvas >>> https://prnt.sc/134fkpo ; Canvas image >>> https://prnt.sc/134flsz ; GameObject >>>> https://prnt.sc/134fnbn ; Cutting >>> https://prnt.sc/134fohv