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 Eltore · May 03, 2014 at 02:37 PM · c#2dvariablelayermasklinecast

Assigning found enemy to target variable

Hello,

The following code is used to see if there is an enemy between the lines. If there is, the boolean will become "true". But how can I assign the found enemy to a variable (public GameObject)?

 enemyInRange = Physics2D.Linecast(rangeStart.position, RangeEnd.position, 1 << Layermask.NameToLayer("Enemy"));

So if there is an enemy and I would have a variable public GameObject target; how could I assign the enemy in the way to that variable?

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
0

Answer by robertbu · May 03, 2014 at 02:39 PM

How about:

 if (enemyInRange != null && enemyInRange.collider != null) {
     someBool = true;
     someGameObject = enemyInRange.collider.gameObject;
 }
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 Eltore · May 03, 2014 at 04:00 PM 0
Share

enemyInRange is a bool at the moment, so I believe this wouldn't work. Thus I have the GameObject target variable for the enemy in the range.

avatar image
0

Answer by Pyrian · May 03, 2014 at 04:09 PM

Physics2D.Linecast returns a RaycastHit2D. I'm a bit surprised you CAN assign that to a Boolean, but you certainly shouldn't if you want to know what is hit. Instead, assign it to a RaycastHit2D, then go through that to get the GameObject.

 RaycastHit2D enemyInRange = Physics2D.Linecast(rangeStart.position, RangeEnd.position, 1 << Layermask.NameToLayer("Enemy"));
 if (enemyInRange != null) {
     GameObject EnemyHit = enemyInRange.rigidbody.gameObject;
 }
Comment
Add comment · Show 5 · 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 Eltore · May 04, 2014 at 01:35 PM 0
Share

Looks great, I'll try it later and come back after that.

avatar image Eltore · May 04, 2014 at 03:41 PM 0
Share

I tried several different ways to do that and always got an error:

 "Cannot implicitly convert type `bool' to `UnityEngine.RaycastHit2D"
avatar image Pyrian · May 04, 2014 at 06:07 PM 0
Share

Post your code. You shouldn't have any bools in it.

avatar image Eltore · May 05, 2014 at 03:23 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class Shoot : $$anonymous$$onoBehaviour {
     public Transform rangeEnd;
     public GameObject enemyHit;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         RaycastHit2D hit = Physics2D.Raycast(transform.position, rangeEnd.position);
         if(hit != null) {
 
         }
     }
 }

That's all at the moment. What it should do after finding the target, is to deal damage to it if player presses $$anonymous$$ouse0 (if it is still in range).

avatar image Pyrian · May 05, 2014 at 08:49 PM 0
Share

Oh, I see. You need to just take out the "!= null", you can simply test "if (hit)".

Here's why.

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

21 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

Related Questions

How do I assign a prefab to a variable with a script? 1 Answer

Multiple Cars not working 1 Answer

unity2d issue with linecasts. 1 Answer

Distribute terrain in zones 3 Answers

how to get a variable like Layermask but for tags 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