Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 yakirh95 · Oct 24, 2020 at 10:09 PM · design

Creating a target for bow&arrows game

Hello guys, been trying to create a target (you know, the round one with red and white colors), and the score you get depends on where you hit the target. but ran into an issue

the way I created it, is with cylinders, one behind another, and mesh colliders, the problem is that the arrow pierces through the first cylinder and crosses to another one behind, and therefore gives the wrong score

any idea how to solve this? tryed modifying the arrow's collider but it did'nt work though, still pierces

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

2 Replies

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

Answer by Hellium · Oct 25, 2020 at 12:12 AM

I would go with a single cylinder collider and distance calculation from center point.

Following code NOT tested

 using UnityEngine;
  
 public class Target : MonoBehaviour
 {
     [System.Serializable]
     public struct Area
     {
         [Min(0)] public float DistanceFromCenter;
         [Min(0)] public int Points;
     }

     // Create an empty GameObject you place at the center of the target
     // The up direction of this empty must be perpendicular to the target surface
     [SerializeField] private Transform center;

     // Fill inspector
     // Order the elements from the smallest to the largest distance
     [SerializeField] private Area[] targetAreas;

     private void OnCollisionEnter(Collision collision)
     {
         Plane targetPlane = new Plane(center.up, center.position); // This may be created once in the Start method if the target does not move or rotate
         Vector3 contactPoint = targetPlane.ClosestPointOnPlane(collision.contacts[0].point); // Taking the 1st contact point may not be the perfect solution
         float distanceFromCenter = Vector3.Distance(contactPoint, center.position);

         for (int i = 0 ; i < targetAreas.Length ; i++)
         {
             if(distanceFromCenter < targetAreas[i].DistanceFromCenter)
             {
                 Debug.Log("The arrow reached the area giving " + targetAreas[i].Points + " points");
                 break;
             }
         }
     }

     // The code below is just to visualise in editor more easily
     private void OnDrawGizmosSelected()
     {
         if (center == null || targetAreas == null)
             return;

         for (int i = 0 ; i < targetAreas.Length ; i++)
         {
             DrawWireCircle(center.position, center.up, targetAreas[i].DistanceFromCenter, 8);
         }
     }

     private void DrawWireCircle(Vector3 center, Vector3 normal, float radius, int sides)
     {
         float deltaAngle = Mathf.PI * 2 / sides;
         Vector3 right = Vector3.Dot(normal, Vector3.up) >= 1
             ? Vector3.Cross(-Vector3.forward, normal).normalized
             : Vector3.Cross(normal, Vector3.up).normalized;
         Vector3 up = Vector3.Cross(right, normal);
         for (int i = 0 ; i < sides ; i++)
         {
             Vector3 from  = right * Mathf.Cos(i * deltaAngle) + up * Mathf.Sin(i * deltaAngle);
             Vector3 to    = right * Mathf.Cos((i + 1) * deltaAngle) + up * Mathf.Sin((i + 1) * deltaAngle);
             Gizmos.DrawLine(from * radius + center, to * radius + center);
         }
     }
 }

alt text


target.png (129.1 kB)
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
avatar image
0

Answer by rhapen · Oct 25, 2020 at 12:05 AM

the way i would do it is create empty gameobject in middle of the target and have the target as one colider then calculate distance between stuck arrow and middle of the target gameobject

using https://docs.unity3d.com/ScriptReference/ContactPoint-point.html for getting the coordinates of arrow

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

140 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

Related Questions

Walls Above Player, but both have same Y Axis 2 Answers

CAN I REALISTICALLY MAKE A MMORPG WITH UNITY 3 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

Script structuring? 2 Answers

UI Design - should you hide it, disable it, or destroy it when it is not in use 2 Answers


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