- Home /
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?
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?
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.
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:
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);
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: ".
Your answer
Follow this Question
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