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 Tristsin · Mar 05, 2013 at 01:40 AM · beginnerraycasthitspherecastraycastall

Using CheckSphere to affect another Game Object?

What we are trying to do is check the radius around character and have those GameObjects set to another material(for example, to simply change the color of the GameObject to blue).

Our game is set on a grid of tiles, 10 in each row, 10 rows (10x10). When it is the Player's turn to move, we want the tiles underneath him to be highlighted blue if they are within his moveRange(which is simply a variable we set). Originally, we sent out a RaycastAll, and it worked well to the left, right, forward, and backwards, but we couldn't get it working with diagonal movements.

Now we're trying to use a CheckSphere, but the problem is that we're not sure how to affect the GameObjects the Sphere is reaching.

Here is the original method, a RaycastAll

 function Test ()
 {
     var layerMask = 1 << 10;
     var hits : RaycastHit[];
     hits = Physics.RaycastAll (transform.position, Vector3(0,0,1), moveRange, layerMask);
     
     for ( var i = 0; i < hits.Length; i++)
     {
         var hit : RaycastHit = hits[i];
         hit.collider.renderer.enabled = true;
         hit.collider.gameObject.renderer.material = gridColor;
     }
 }

The layerMask was set to only collide with the tiles, so it didn't change anything but tiles.

Here is what we have so far with a CheckSphere

 var layerMask = 1 << 10;
     if ( Physics.CheckSphere(transform.position, moveRange, layerMask) )
     {
     
     }

In the original, we affected the GameObjects that the Ray hit by using a Raycasthit. We have no idea how to do so with a CheckSphere.

Any help is appreciated, or if anyone has a better method of achiving our goals that we be appreciated as well.

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

Answer by robertbu · Mar 05, 2013 at 02:19 AM

You can use SphereCastAll() or CapsuleCastAll():

 RaycastHit[] hits = Physics.SphereCastAll(transform.position, radius, Vector3.forward, 0.001f);

When I last tried, it failed if the distance (last parameter) was 0.0.

Comment
Add comment · Show 3 · 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 Tristsin · Mar 05, 2013 at 02:34 AM 0
Share

We've tried using SphereCastAll, but it doesn't seem to work at all. Or we simply don't understand what's it doing. It doesn't send to be casting out a Sphere from the Player's position(transform.position)

avatar image robertbu · Mar 05, 2013 at 02:53 AM 0
Share

Just to be absolutely sure, I tested the line of code above before I posted it. It worked fine. The first parameter should be the position of your player. The last needs to be non-zero (but can be small like I have it here). I'm not sure why it has to be non-zero other than in testing it would not return any hits if it was 0.0.

avatar image Tristsin · Mar 05, 2013 at 03:08 AM 0
Share

I don't understand. I'm going to post another comment with the code, and a screenshot showing what it's doing and maybe you can help explain to me the problem.

avatar image
0

Answer by Tristsin · Mar 05, 2013 at 03:26 AM

 function SphereTest ()
 {
     var gos : GameObject[];
     gos = GameObject.FindGameObjectsWithTag("Tiles");
     
     for ( var go : GameObject in gos)
     {
         var thisTrans = transform.position;
         var goTrans = go.transform.position;
         var hits : RaycastHit[];
         var layerMask = 1 << 10;
         
         hits = Physics.SphereCastAll(thisTrans, a, Vector3(1,0,1), 0.1);
 
            for (var i = 0;i < hits.Length;i++) 
         {
             var hit : RaycastHit = hits[i];
             var renderer =  hit.collider.renderer;
                 if (renderer) 
                 {
                        renderer.material.shader = Shader.Find("Transparent/Diffuse");
                     renderer.material.color.a = 0.3;
                 }
         }

This is a reply to a poster. I didn't know how else to reply with code and a screenshot. The problem is, the "sphere" doesn't seem to be a sphere, and nothing I do makes it so. Maybe I'm just not understanding the concept, but to me, Casting a Sphere means casting a Sphere(circle) from the transform.position of my player(where this script is attached) and then returning the tiles it hits, and then rendering those transparent. It seems to be doing..... I don't know. I've tried every possible solution to make sense of it. I want to make a circle around my player's position, and any of the tile's collider that are within that sphere's range, I want it to highlight them blue.

In the Screenshot, my Player Character is the Cube that is off to itself. The Cubes in the middle are currently just Cubes, nothing more.

alt text


1.png (298.8 kB)
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 robertbu · Mar 05, 2013 at 04:08 AM 0
Share

SphereCastAll() takes a sphere shape and pushes it through the scene on the vector specified for the distance specified, noting any colliders it hits along the way. Are you sure that thisTrans is the transform of the Player? What is the scale of your game (i.e. is the 0.1 you are using for distance perhaps a big number)?

Before I posted, I attached a script with the above line of code, outputting the number of colliders it found each frame. Then I tossed a bunch of game objects into the scene and moved the "player" around to see what they sensed.

avatar image robertbu · Mar 05, 2013 at 05:18 AM 0
Share

I played with SphereCastAll() and you are right. Its behavior is not what is expected. I'm sorry for the bad advice. Is there any reason you are not simply doing a distance check? I tried it out with a array of cubes, and it worked fine:

 GameObject[] argo = GameObject.FindGameObjectsWithTag("Tile");
         for (int i = 0; i < argo.Length; i++) {
             if ((argo[i].transform.position - transform.position).magnitude < 1.8)
                 argo[i].renderer.enabled = false;

 
avatar image robertbu · Mar 05, 2013 at 05:36 AM 0
Share

If you really need the SphereCastAll() to work, you can use it in the way it was intended. Pick a position for the start point that is on the Y axis of the player but with the given radius will not touch the tiles. Then use Vector3.down for the direction and give it a distance high enough to push down through the tiles. Worked just fine in my tests.

avatar image Tristsin · Mar 05, 2013 at 07:16 AM 0
Share

I'll give this a try, thanks. There's no particular reason to use SphereCastAll, I was actually hoping to find a better way anyway.

avatar image Tristsin · Mar 05, 2013 at 07:26 AM 0
Share

Alright, that works fairly well.

The problem is, none of these seem to be what we want. We would like a way to pin-point which tiles will be highlighted. With yours, it works alright, but we truly have control over the movement(like for example, if we wanted to restrict the movement diagonally but not vertically or horizontal. We're also used a OverlaySphere, but it gives about the same results as that.

Also did this:

 function SphereTest ()
 {
     var gos : GameObject[];
     gos = GameObject.FindGameObjectsWithTag("Tiles");
     
     for ( var go : GameObject in gos)
     {
         var curDistanceX = Vector3.Distance(Vector3(go.transform.position.x, 0 ,go.transform.position.z), (transform.position.x, 0, transform.position.z));
         var curDistanceY = go.transform.position.y;        
         
         if ( curDistanceX <= moveRange )
         {
             go.renderer.material.color = Color.blue;
         }
     }
 }

but, again it didn't truly give us control over it. We are stumped honestly. We've been working on it for over 8 hours straight with pretty much every solution we come up with being not just right.

$$anonymous$$ore examples:

$$anonymous$$ost of the solutions gave us something like this.

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ]

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ]

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ]

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ]

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ]

[ ],[ ],[ ],[X],[X],[X],[ ],[ ],[ ],[ ]

[ ],[ ],[X],[X],[X],[X],[X],[ ],[ ],[ ]

[ ],[X],[X],[X],[X],[X],[X],[X],[ ],[ ]

[ ],[X],[X],[X],[X],[X],[X],[X],[ ],[ ]

[X],[X],[X],[X],[P],[X],[X],[X],[X],[ ]

When in reality we would want something like

[[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ]

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ]

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ]

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ]

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ]

[ ],[ ],[ ],[ ],[X],[ ],[ ],[ ],[ ],[ ]

[ ],[ ],[ ],[X],[X],[X],[ ],[ ],[ ],[ ]

[ ],[ ],[X],[X],[X],[X],[X],[ ],[ ],[ ]

[ ],[X],[X],[X],[X],[X],[X],[X],[ ],[ ]

[X],[X],[X],[X],[P],[X],[X],[X],[X],[ ]

Where your max movement from the starting position would be 4 blocks away, hopefully this explains it well.

The diagram didn't come out lined up like in notepad, but hopefully you can make it out. Empty spaces are unmovable, spaces with X are movable, and P represents my Player Character

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

10 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

Related Questions

RaycastAll/RaycastHit - select "outermost" collider among nested colliders? 1 Answer

Spherecast returning false if character is too close to gameobject 0 Answers

Sphercast hitinfo TextureCoord rounded? 1 Answer

Raycast problems! 1 Answer

CircleCastAll layermask doesn't seem to work... 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