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 randallman · Jan 21, 2014 at 02:59 AM · collisionraycastgridray

what's wrong with my raycast object report code?

I'm working on a turn based grid game. think checkers. I'm really proud of myself for making the randomized grid map without any help from plugins, but the trouble started when I tried to make the objects start interacting. What I need this code to do is create a "go right" button which sends a ray from the object to the right one unit. if the raycast hits an object tagged enemy, I need the player object to move two units like he's jumping him (checkers). otherwise I just need the player object to move one space.

As you can see, I am having trouble just getting the ray to report an object. I have tried many solutions: from triggerEnters to Rays; from kinematic onTriggers to box collider rigidbodies. The only thing that seems to be consistent is that either collisions are ALWAYS happening or NEVER happening.

 function OnGUI(){
     if(myTurn){
         //move/jump right
         if(GUI.Button(Rect(Screen.width/2 + 100, Screen.height/2, 75,75), moveRightButton)){
             var hit : RaycastHit;
             var myRay = new Ray(myPos, Vector3.right);
             if(collider.Raycast(myRay, hit,1)){
                 if(hit.collider.gameObject.tag == "Enemy"){
                 Debug.Log("this is awkward");
                 }
             }
         }
     }
 }


Mostly, I just need a fresh pair of eyes to make sure my ray is in the right format. If you can think of a better way to do this, let me know. I also want to make the player object keep "jumping" if more than one enemy is lined up. If you're feeling abstract, do you have any ideas for a looping function that repeats the jump routine until there's no more enemies? But most importantly, what's going on with my Raycast? Thank you very much for your time.

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
0

Answer by robertbu · Jan 21, 2014 at 03:05 AM

One issue is that you are using Collider.Raycast(). Collider.Raycast() only casts against a specific collider. In this case you are Raycasting against the object this script is attached to. Since you are inside the object and mesh are one sided, you won't hit anything. Replace your 'collider.Raycast' with Physics.Raycast:

 if (Physics.Raycast(myRay, hit, 1)) {
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 randallman · Jan 21, 2014 at 04:38 AM 0
Share

Thank you! I will give it a try. I started off using Physics.Raycast, but couldn't get the layermask to work. Checking the hits for a tag wasn't producing good results either, but I'm going to try to move the objects off of the grid tiles in the z - axis, so there's nothing else for the ray to hit. Will report back on that. Can I tap you for ideas on how to make a function like... press button, raycast this direction, move this direction D units, where D is the amount of enemy raycastHits + 1.

Thanks again!

avatar image robertbu · Jan 21, 2014 at 04:49 AM 0
Share

Layer masks are tricky since they are a bit set and not an integer. You have to set the right bit in the mask for it to work correctly. You don't include the code where you initialize your mask, so I cannot check it. Here is a bit of code that will jump over the thing that is found by the ray cast. It uses world directions not directions relative to the "character." I'm not sure of the design of your game:

 function Update() {
     
     var hit : RaycastHit;
     var pos : Vector3;
     
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)) {
         if (Physics.Raycast(transform.position, Vector3.right, hit)) {
             pos = hit.transform.position;
             pos.x += 1.0;
             transform.position = pos;
         }
     }
         
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)) {
         if (Physics.Raycast(transform.position, Vector3.left, hit)) {
             pos = hit.transform.position;
             pos.x -= 1.0;
             transform.position = pos;
         }    
     }
 }
avatar image randallman · Jan 21, 2014 at 09:23 AM 0
Share

Thank you very much for your help! I needed to write it out before I found a solution and you really cleared up rays for me. I can't copy the solution here because it's massive, but basically I just raycast in the direction and move and then do it again, up to five times. The only thing left to do now is copy that to 8 different directions. Woo! Thanks again

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

18 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

Related Questions

Detecting Raycast collision on an enemy to deal damage 1 Answer

Moving cubes into a grid formation 1 Answer

Fall collision force with help of raycast 0 Answers

Someone can help with Ray/Collide? 1 Answer

Unreliability of Physics.Raycast.Distance for player movement? 0 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