- Home /
split object when player touches anywhere in the screen
i made a game where u throw knives , i already made a throwing script , it works fine , but how can i add a knife with an ability to split into 3 knives when player touches anywhere (after throwing )? see code sample and image for reference
void Start()
{
rb = GetComponent<Rigidbody2D>();
//rb.rotation = 45f;
//AudioSource throwsound = gameObject.AddComponent<AudioSource>();
shu_throw = GetComponent<AudioSource>();
audioSource = GetComponent<AudioSource>();
RotShu.SetActive(false);
alphalevel = 1f;
//InvokeRepeating("spawntrail", 0.05f, 0.05f);
}
void Update()
{
if (IsPressed)
{
Vector2 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//------
//-------
if (Vector3.Distance(mousepos, hook.position) > maxdragdis)
{
rb.position = hook.position + (mousepos - hook.position).normalized * maxdragdis;
}
else
rb.position = mousepos;
}
}
void OnMouseDown()
{
IsPressed = true;
rb.isKinematic = true;
}
void OnMouseUp()
{
IsPressed = false;
rb.isKinematic = false;
StartCoroutine(Release());
//shu_throw.Play();
RotShu.SetActive(true);
GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0);
the throwing is done , all i want is a script that allows detection of player touch so th eobject splits after throwing , just to make this clear and simple , seen that blue bird in angry birds ? when you shoot it u click on screen whenever you want and the birds splits into 3 ? thats what i want to make, i want just the click detection part i will take care of the rest , and thank you
Answer by outramjamie_unity · Oct 30, 2020 at 06:38 PM
You could use Input.GetMouseDown() in an update function attached to the base knife:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("MouseClicked");
}
}
This is a bit crude but it works. For somthing more advanced like If you want it to ignore if you've clicked on something you could write your own Input manager to fire a raycast when Input.GetMouseButtonDown(0) is detected and tigger an event if nothing is hit, Here's an example of raycasting with the clicked position from something I'm working on:
public class InputManager : MonoBehaviour
{
//Define interactions in inspector
[SerializeField] LayerMask mouseInteractables = default;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
RaycastHit2D[] hits = GetSortedRayIntersectionAll(CameraController.activeCamera.ScreenPointToRay(Input.mousePosition));
//Trigger on click event for object
if (hits.Length > 0) {
CustomBehaviour trigger = hits[0].collider.gameObject.GetComponent<CustomBehaviour>();
if (trigger != null)
trigger.MyOnMouseDown();
else Debug.LogWarning("Clicked collider does not have a CustomBehaviour script");
}
}
if (Input.GetKeyUp(KeyCode.Mouse0))
{
RaycastHit2D[] hits = GetSortedRayIntersectionAll(CameraController.activeCamera.ScreenPointToRay(Input.mousePosition));
//Trigger on click event for object
if (hits.Length > 0)
{
CustomBehaviour trigger = hits[0].collider.gameObject.GetComponent<CustomBehaviour>();
if (trigger != null)
trigger.MyOnMouseUp();
else Debug.LogWarning("Clicked collider does not have a CustomBehaviour script");
}
}
}
RaycastHit2D[] GetSortedRayIntersectionAll(Ray ray)
{
RaycastHit2D[] hits = Physics2D.GetRayIntersectionAll(ray, Mathf.Infinity, mouseInteractables);
int[] keys = new int[hits.Length];
for (int j = 0; j < hits.Length; j++)
{
keys[j] = hits[j].collider.gameObject.layer;
}
Array.Sort(keys, hits);
return hits;
}
}
my code in void update already has an if statement i don't know how to put it inside .....do you recommend writing this in another script and attach it to the same object ? one script has knife function and one has the ability ? @outramjamie_unity
Sorry for the slow response. I'd handle the splitting in its own $$anonymous$$onoBehaviour script attached to the first projectile (called something like ProjectileSplitBehaviour). The job of this script would be only to detect when they click then split the projectile its attached to.
Your answer
Follow this Question
Related Questions
Referencing a variable in another script possibly not working? 0 Answers
how can you split an object in half 1 Answer
Shooting with variable vector3 0 Answers
Manipulating Instanced Object 2 Answers