Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by eni_cay · Oct 30, 2020 at 05:46 PM · instantiateobjectshootingobject referencesplit

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 alt text

nytntynt.png (41.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
     }
     
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eni_cay · Oct 30, 2020 at 08:12 PM 0
Share

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

avatar image outramjamie_unity eni_cay · Nov 02, 2020 at 02:18 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

179 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shooting with variable vector3 0 Answers

Referencing a variable in another script possibly not working? 0 Answers

how can you split an object in half 1 Answer

Problem with Intantiate using uLink! 2 Answers

Physics on shooting objects/effects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges