• 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 martygrof3708 · Nov 20, 2019 at 05:08 AM · raycastgetcomponentinterface

How can I detect for 2 different component types using an interface on RayCast Hit?

I have a game where you swipe to hit balls, (basically). The game will also have power ups which the player can tap on. When the user swipes and hits a ball, a force is calculated and sent the the ball. However, the power ups do not need a force applied, only need to be told they are hit. I am new to interfaces and probably not doing it right, but I am trying to have one interface where both the balls and power ups can inherit from. It will have a "OnHit" function which is called on the raycast script. If it is a ball, I will need to send a force to the OnHit(), but if its a powerup, I dont need any parameters.

 private void CastRay (int i)
 {
     Ray raycast = Camera.main.ScreenPointToRay (Input.touches [i].position);
     RaycastHit rayHit;
     ISwipeable swipe;

     if (Physics.Raycast (raycast , out rayHit) && ((swipe = rayHit.collider.GetComponent<ISwipeable> ()) != null))
     {
         Vector2 direction = rayHit.collider.transform.position - rayHit.point;
         float accel = Input.GetTouch (i).deltaPosition.magnitude / Input.GetTouch (i).deltaTime;
         Vector2 force = direction * accel;

         swipe.OnSwipe (force);
         touchHit [i] = true;
     }
 }

Basically I am looking for the most efficient and proper way to check if the raycasthit collider has a "hittable" component, and to check weather I should send a force to it if it is.

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
1
Best Answer

Answer by fafase · Nov 20, 2019 at 07:11 AM

It's all about how the interface is implemented. The consumer class (the one using the interface object) should not care about or know what is done in the interface, so it would call it and that's it. Then in the implementation it either uses or not the parameter.

Now if you want to prevent some computation because the force is not needed, you can extend your interface:

 public interface ISwipeable
 {
         void OnSwipe (float force);
         void OnHit();
         bool UseForce{  get; }
 }

 public class Ball: MonoBehaviour, ISwipeable
 {
       public bool UseForce{ get { return false;  } }
 }
 public class PowerUp: MonoBehaviour, ISwipeable
 {
       public bool UseForce{ get { return true;  } }
 }

  if (Physics.Raycast (raycast , out rayHit) && ((swipe = rayHit.collider.GetComponent<ISwipeable> ()) != null))
  {
      if(swipe.UseForce)
     {
            Vector2 direction = rayHit.collider.transform.position - rayHit.point;
            float accel = Input.GetTouch (i).deltaPosition.magnitude / Input.GetTouch (i).deltaTime;
            Vector2 force = direction * accel;
            swipe.OnSwipe (force);
      }
      else
     { swipe.OnHit(); }
      touchHit [i] = true;
  }

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 martygrof3708 · Nov 20, 2019 at 07:18 AM 0
Share

Awsome! Thanks alot, I was trying to put a boolean in the interface but it wasn't working. I didnt realize you need a getter ins$$anonymous$$d.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

165 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

Related Questions

Multi Coloured Buttons 0 Answers

Why isn't this raycast working? 2 Answers

Getting UV coordinate with GraphicRaycaster 2 Answers

How do I change a clicked on GameObjects variable when a boolean (Next Turn) is true? 1 Answer

getting position from a script 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