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 Ekta-Mehta-D · Mar 22, 2013 at 06:37 AM · raycastshootinggrid

2D : RayCast Problem

I have grid structure and i am arranging sphere in closest empty spaces like bubble shooter.

Now for shooting , i am using raycast concept.

![alt text][1]

In grid i have problem is that , I do not want to allow my sphre to cross the grid cell filled with blue color.

But bcz i have used raycast which takes mouse position. And in my case it ignores the in between sphere and collides with the sphere available at mouse position and takes the nearest empty position. But i do not want to allow them to cross. Same ting applies to newly assigned sphere, if they are in between the way then they also should not be crossed. So what do i need to do??

My Shooting Script:

 function Update ()
 {
         emptyObject = GameObject.FindGameObjectsWithTag("Empty");
         for(var i : int = 0 ; i < emptyObject.Length ; i++)
         {
             emptyList.Add(emptyObject[i].transform);
         } 
         Debug.Log("Length empty: " + emptyObject.Length);
         if(transform.parent == null)
         {
               var hit : RaycastHit ;
               var ray : Ray  = Camera .main .ScreenPointToRay (Input.mousePosition );
               if(Physics.Raycast (ray, hit)){     
                  var tempx = hit.transform.position.x;
                  var tempy  = hit.transform.position.y;
                  target = Vector3(tempx,tempy,0);
               }
                 if(target != null){
                 var moveAmount : float = 5000 * Time.deltaTime;
                 transform.rigidbody.AddForce(transform.TransformDirection(target) * moveAmount);            
              }  
          }
 }

On collision :

 function OnCollisionEnter(collision : Collision )
 {        
         if(transform.parent == null)
         {      
                      var contact : ContactPoint = collision.contacts[0];
                         var pos : Vector3 = contact.point;    
                         var t : Transform = ClosestTransform(pos);
                         transform.position = t.position;
                         transform.rotation = t.rotation;
                         transform.rigidbody.velocity = Vector3.zero;
                         transform.rigidbody.angularVelocity = Vector3.zero; 
                         transform.rigidbody.isKinematic = true;
                         transform.parent = go.gameObject.transform;
                         t.gameObject.tag = "";
                 
                               
         }            
 }

Function :

 function ClosestTransform(curr : Vector3) : Transform
 {
     var fDist : float = Mathf.Infinity;
     var v3Curr : Vector3 = curr;
     var trOut : Transform = emptyList[0];
     
     for (var trans : Transform in emptyList) {
            var fT : float= (trans.position - v3Curr).sqrMagnitude;
            if (fT < fDist) {
              trOut = trans;
              fDist = fT;
            }
         }
     return trOut;
 }

Please help me. Thanks for your help and support..

8985-hexgridgame2.jpg (93.1 kB)
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
Best Answer

Answer by Fattie · Mar 22, 2013 at 07:15 AM

Ekta, just so you know.

Many programmers would do a game like this, not using physics at all.

Imagine your hex number 371.

Six hexs are touching it. In fact it might be 370, 372, 339, 340, 401, 402

Note that normally you can figure-out the six, using a fairly simple formula.

Or, you can do this: for each hex (say 370), just keep the six neighbors in variables.

So, make a little structure with fields like "up and to the left" "right hand neighbor" etc. Compute all that at development time, or even just when the game launches.

It is then extremely easy for you to know neighbors during play.

While it is definitely possible (and possibly sensible) to use raycasting for such a game, it may be better to just do it logically, with some arrays.

Hoep it helps

Comment
Add comment · Show 2 · 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 Ekta-Mehta-D · Mar 22, 2013 at 07:20 AM 0
Share

But my problem is that my bullet ignores in between sphere. And directly goes to the mouse position. what do i do for this problem ?

avatar image Fattie · Mar 22, 2013 at 07:24 AM 0
Share

HOW BIG are your sphere colliders? it may be you have to make them all big enough that they re just about touching. It could be that simple.

$$anonymous$$ake screen shot OF YOUR EDITOR showing the rig ok?

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

11 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

Related Questions

First Person Controller Target Shooting With Gun 1 Answer

Object Does not Destroyed While Shooting 1 Answer

AI Shooting for Aircraft 1 Answer

A node in a childnode? 1 Answer

Why does the wrong enemy die with this raycast? C# 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