Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Revifle_Monarch_Studios · Apr 21, 2019 at 09:38 PM · gameobjectraycastrayradius

Detect gameobjects via radius [PROBLEM]

Hello, thank you for taking time. I've got a maybe not that simple question. It's about the radius and these damn Physics.OverlapSphere things. I'm just not able to do it.

Short Introduction: I'm making a "Settlers of Catan" and "Civilization" inspired Hex-Game with creating custom worlds and so on. I got to the units now. I'm fine with how you can send them to positions, you can select them, send them somewhere (needs still improvement, but that project is more a test than a try to make a game). But I came to a problem now. Of course I don't want the player to be able to send his units anywhere, like to the end of the map, in one turn. My idea of making the player able to send his unit only to neighbouring province was to create a radius around the unit. He is only able to be set somewere within a distance of say 10f. I actually don't care for the number now, I want to know how to implement it. My script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BasicUnitScript : MonoBehaviour
 {
 
     public Camera cam;
 
     Vector3 newPosition;
     public LayerMask seaTileMask;
 
     public HexagonWorldCreation worldScript;
 
     public bool unitSelected;
     public bool unitSelectable;
 
     // Start is called before the first frame update
     void Start()
     {
         unitSelected = false;
         unitSelectable = true;
 
         newPosition = transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
 
         int layerMask = 1 << 9;
         layerMask = ~layerMask;
 
         if (Input.GetMouseButtonDown(0))
         {
             Ray unitSelectionRay = cam.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast(unitSelectionRay, out hit))
             {
                 if ((hit.transform == this.gameObject.transform) && (unitSelectable == true))
                 {
                     unitSelected = true;
                 }
             }
 
         }
 
         if (Input.GetMouseButtonDown(1))
         {
             Ray unitMovementRay = cam.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit2;
 
             if (Physics.Raycast(unitMovementRay, out hit2))    // If something doens't work, check the 100!
             {
                 if ((unitSelected == true) && (hit2.transform.tag != "SeaTile"))
                 {
 
                     unitSelected = false;
                     unitSelectable = false;
 
                     newPosition = hit2.point;
                     transform.position = newPosition;
                     this.gameObject.transform.position = new Vector3(hit2.point.x, 18.7f, hit2.point.z);
                     
                 }
             }
 
             Debug.Log("hit2.point: " + hit2.point + " transform.position:" + transform.position + " hit tag: " + hit2.transform.tag);
         }
     }
 }
 

Could anyone please implement it for me, 'cause I got no idea. I looked the doc and the other threads up, but it didn't worked. If you could, thank you!

Note: Please no layerMasks!

Comment
Add comment · Show 5
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 KevRev · Apr 22, 2019 at 12:14 AM 1
Share

You want:

Vector3 origin

Vector3 target (mouse point)

Ray ray (from origin to target)

Float distance (10.0f)

ray.GetPoint(distance);

This should give you a vector3 location at the distance you require.

avatar image Revifle_Monarch_Studios KevRev · Apr 22, 2019 at 03:48 AM 0
Share

Nice, thank you!

avatar image Revifle_Monarch_Studios KevRev · Apr 22, 2019 at 10:04 AM 0
Share

But I still don't know how to implement it

avatar image KevRev Revifle_Monarch_Studios · Apr 22, 2019 at 11:21 AM 0
Share

What have you tried so far?

Personally I'd probably do a ray to screenpoint at the max distance, and if that ray collides with a selectable hex-tile, highlight it. Then as you move the mouse around you'll highlight just the tiles that are within range.

Show more comments

1 Reply

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

Answer by InfixionGames · Apr 22, 2019 at 01:46 PM

I hope this can help you

             //The distance between my target (player) and my transform (enemy)
                 float distance = Vector2.Distance(target.position, transform.position);
             //If the distance is less than the detectionRange(detectionRange is a float)
                 if (distance < detectionRange)
                 {
                 //Do something
                     Movement();
                 }
 
 


Comment
Add comment · Show 29 · 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 Revifle_Monarch_Studios · Apr 22, 2019 at 03:37 PM 0
Share

Thank you, but could you please explain to me how to use it with a ray? And where is detectionRange defined? But still, thank you for helping me! So, you know, it's about unit movement in Hexagon-Provinces. I want that the unit can only go to bordering provinces. So, the target.position is the position where the ray shot too, isn't it? And detectionRange is the Range in within the provinces are detected? I'm going to try it!

avatar image InfixionGames Revifle_Monarch_Studios · Apr 22, 2019 at 04:00 PM 0
Share

You define the detectionRange at the top of your script (this checks for nearby provinces), inside the if statement you would do your raycasting. If you want to move the units to a hexagon you would need to do some casting to find which hexagon was click and move the units to the center of that hexagon. So here is some more clear code.

               float detectionRange = 10f;
                 //The distance between my target (this would be your hexagon) and my transform (this would be your unit)
                     float distance = Vector2.Distance(target.position, transform.position);
                 //If the distance is less than the detectionRange
                     if (distance < detectionRange)
                     {
                     //Here you would do the raycasting
                         
                     }


avatar image Revifle_Monarch_Studios InfixionGames · Apr 22, 2019 at 04:14 PM 0
Share

Yeah, I could copy and paste it now, but I want to understand this, and I didn't, and I will ask till I understood it, if I ask you or any other person. I'm still bothering with when

 if (distance < detectionRange)

has to do its job. It makes no sense for me. Why does it checks / detect it even before the ray is shot? I'm not saying that your code is wrong and bad and not logical, but I need some explaination. I would say that the

 if (distance < detectionRane)

should be in the clause of the ray, in here:

 if (Physics.Raycast(unit$$anonymous$$ovementRay, out hit2))    // Check the y if not working
             {
                 if ((unitSelected == true) && (hit2.transform.tag != "SeaTile"))
                 {
 
                     unitSelected = false;
                     unitSelectable = false;
 
                     newPosition = hit2.point;
                     transform.position = newPosition;
                     this.gameObject.transform.position = new Vector3(hit2.point.x, 18.7f, hit2.point.z);
                     
                 }
             }

Show more comments

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

192 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Shoot off multiple raycasts from 1 object? 2 Answers

How to detect if a raycast ray stop hitting an object 1 Answer

How to damage gameobject via raycast 1 Answer

click on a game object that script isn't attached to 1 Answer

Ray cast collider recognition and execution of a script attached to hit object 1 Answer


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