Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 kristoft · Aug 15, 2015 at 02:03 PM · 2d3dscore

How i can calculate points when hitting dart target?

I have 2D sprite of target with few areas of different score and 3D dart which hits target. And i need to know in what zone dart hits. How i can do that?

Comment
Add comment · Show 2
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 getyour411 · Aug 16, 2015 at 11:11 AM 0
Share

Explain the mechanics of the dart throw you intend to use. Will the darts use actual physics? Do you have a crosshair where player is ai$$anonymous$$g which will have some jitter or randomness added?

avatar image sillyrabbit · Aug 16, 2015 at 11:58 AM 0
Share

You need to post an image, or some code. At the least, as getyour411 says, describe further. As it is, sounds like you could use colliders representing each zone to detect where the dart is.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Evansbsr · Aug 16, 2015 at 06:56 PM

One way is to get the position which the dart hit relative to the dartboard itself.

First create a gameobject with a SpriteRenderer for the dartboard and add a CircleCollider2D that matches its size. Add this script to the dartboard:

 using UnityEngine;
 using System;
 
 public class Dartboard : MonoBehaviour {
 
     SpriteRenderer   sprite;
     CircleCollider2D circCollider;
     public Color onColor, offColor;
 
     Vector2  position;
     Vector3  touchPoint, localTouchPoint;
 
     Camera   mainCamera;
 
     void Awake() {
         mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
         sprite = GetComponent<SpriteRenderer>();
         circCollider = GetComponent<CircleCollider2D>();
     }
     void LateUpdate () {
         sprite.color = Color.white;
         if(Input.GetMouseButton(0)) {
             touchPoint   = mainCamera.ScreenToWorldPoint(Input.mousePosition);
             if(Physics2D.OverlapCircle(touchPoint, .01F)) {
                 sprite.color = onColor;
                 localTouchPoint   = transform.InverseTransformPoint(touchPoint);
 
                 position.x = localTouchPoint.x  / circCollider.radius;
                 position.y = localTouchPoint.y  / circCollider.radius;
             } else {
                 sprite.color = offColor;
             }
         }
     }
 
     void OnGUI() {
         GUI.Box(new Rect(30,30,100,40), position.ToString() );
     }
 }

What's happening?

 touchPoint   = mainCamera.ScreenToWorldPoint(Input.mousePosition);

This part converts the point which the user has clicked from screen co-ordinates to world co-ordinates.


 if(Physics2D.OverlapCircle(touchPoint, .01F)) {

Determines whether the point the user has clicked overlaps any colliders.


 localTouchPoint   = transform.InverseTransformPoint(touchPoint);

Converts the touchPoint variable from world space to local space. This means that the hit position is now relative to the dartboard gameobject's position.


 position.x = localTouchPoint.x  / circCollider.radius;
 position.y = localTouchPoint.y  / circCollider.radius;

This clamps the resulting position variable's values within 0 and 1 for easier readibility.

Here is a screenshot showing it in action:

alt text


dartboard-test-1.jpg (58.4 kB)
Comment
Add comment · Show 1 · 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 kristoft · Aug 16, 2015 at 08:57 PM 0
Share

Thank you. Problem was in getting right local coordinates with Transform.InverseTransformPoint(). I used transform.InverseTransformPoint(mouse.position) ins$$anonymous$$d of

 localTouchPoint   = transform.InverseTransformPoint(touchPoint);

avatar image
0

Answer by 2Mtech ADMIN · Aug 16, 2015 at 03:25 PM

Add a box collider to the darts and a rigid body and mark "is trigger" on the box collider, and add a box collider on the target and mark "is trigger" on it too. Add this script to the dart:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class YOURSCRIPTNAME : MonoBehaviour {
 
     public Text countText;
 
     private int count;
 
     void Start () {
         count = 0;
         SetCountText ();
     }
 
     void Update () {
     
     }
     void OnTriggerEnter(Collider other){
         if (other.gameObject.CompareTag ("Target")) {
             other.gameObject.SetActive (false);
             count = count + 1;
             SetCountText ();
         }
     }
 
 
     void SetCountText () {
     countText.text = "Points: " + count.ToString();
     
         }
     }
 }

Add a tag to the target called "Target".

Make a UI canvas and in the canvas a text called countText, don't put any text in it.

That should make the count text add +1 every time the dart hits the target. If you want it to add different points where it hits put a box collider on each part of the target, and copy the script to the box collider. If you want to add more or less points change the part in the script that says "count + 1;", and to change the text of the counter change "Points: ".

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2d or 3d for better perform on mobile devices? 1 Answer

2D object look at 3D object 0 Answers

How to show score after enemy death.,How to show score after the death of the enemy. 1 Answer

2d sprite bacground motion? 2 Answers

UDP app signature? 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