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 kizzagarhu · Feb 13, 2015 at 10:00 AM · tutorialgenericroguelikedatatype

How does Unity figure out generic component type as Wall in Project: 2D Roguelike tutorial?

Unity released a tutorial for 2d roguelike game a couple days ago: http://unity3d.com/learn/tutorials/projects/2d-roguelike

In the project movement of both enemy and player units are handled by MovingObject script, which first attempts the move by checking collisions. If collisions return true, a generic component is passed, which later is assumed to be either a Wall or Player component to deal damage. Here is the AttemptMove code sample from MovingObjects from the project:

         protected virtual void AttemptMove <T> (int xDir, int yDir)
             where T : Component
         {
             //Hit will store whatever our linecast hits when Move is called.
             RaycastHit2D hit;
             
             //Set canMove to true if Move was successful, false if failed.
             bool canMove = Move (xDir, yDir, out hit);
             
             //Check if nothing was hit by linecast
             if(hit.transform == null)
                 //If nothing was hit, return and don't execute further code.
                 return;
             
             //Get a component reference to the component of type T attached to the object that was hit
             T hitComponent = hit.transform.GetComponent <T> ();
             
             //If canMove is false and hitComponent is not equal to null, meaning MovingObject is blocked and has hit something it can interact with.
             if(!canMove && hitComponent != null)
                 
                 //Call the OnCantMove function and pass it hitComponent as a parameter.
                 OnCantMove (hitComponent);
         }

How does either Wall or Player component picked and not say Transform, SpriteRenderer or any other component?

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

In the tutorial of the 2D Roguelike game, where is the player gameobject instantiated in the code? 2 Answers

2D Roguelike - moving onto enemies 3 Answers

For loop in "Writing the Board Manager" 2d Roguelike confusion 1 Answer

How to make a slider menu 1 Answer

Ray (Gunshot) not visible, shadow is, why? 3 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