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 Darkkingdom · Oct 01, 2014 at 10:55 PM · collisionraycastdirection

Collision InFront?

Hey guys :)

I have the following Situation:

I'm working on a topdown 2D-Game with an 8-Way Movementsystem. So I wanted to write a script that checks, if the collision which my raycast notifies is in the Front of my GO. (Thats for things like stop moving when your walking against a wall and so on)

The "Check in Front" Script consists of 3 parts.

At first my "GetCollisionDirection" void in my raycast script. (The problem here is that I cant make any get-functions from the other scripts because I use the same raycastscript for all my GOs) The raycast script works and set the bool from the direction, which the collision comes from, to true.

         void GetCollisionDirection()
         {
             if (collisionAny == true) {
                 if (collisionUpLeft == true) {
                     collisionDirection = 8;
                 }
                 if (collisionDownLeft == true) {
                     collisionDirection = 7;
                 }
                 if (collisionDownRight == true) {
                     collisionDirection = 6;
                 }
                 if (collisionUpRight == true) {
                     collisionDirection = 5;
                 }
                 if (collisionRight == true) {
                     collisionDirection = 4;
                 }
                 if (collisionLeft == true) {
                     collisionDirection = 3;
                 }
                 if (collisionUp == true) {
                     collisionDirection = 2;
                 }
                 if (collisionDown == true) {
                     collisionDirection = 1;
                 }
     
             
                 //Check if more than 1 true
               //The next part is my main problem I think :(
                 if (collisionDown == true && collisionDownRight == true && collisionDownLeft == true)
                 {
                     collisionDirection = 1;
                 }
                 if (collisionUp == true && collisionUpRight == true && collisionUpLeft == true)
                 {
                     collisionDirection = 2;
                 }
                 if (collisionLeft == true && collisionDownLeft == true && collisionUpLeft == true)
                 {
                     collisionDirection = 3;
                 }
                 if (collisionRight == true && collisionDownRight == true && collisionUpRight == true)
                 {
                     collisionDirection = 4;
                 }
                 if((collisionUp == true && collisionUpRight == true && collisionRight == true) ||
                    (collisionUp == true && collisionUpRight == true && collisionUpLeft == false) || 
                    (collisionRight == true && collisionUpRight == true && collisionDownRight == false))
                 {
                     collisionDirection = 5;
                 }
                 if((collisionDown == true && collisionDownRight == true && collisionRight == true) ||
                     (collisionDownRight == true && collisionRight == true && collisionUpRight == false) || 
                     (collisionDown == true && collisionDownRight == true && collisionDownLeft == false))
                 {
                     collisionDirection = 6;
                 }
                 if((collisionDown == true && collisionDownLeft == true && collisionLeft == true)    ||
                     (collisionDown == true && collisionDownLeft == true && collisionDownRight == false) || 
                     (collisionLeft == true && collisionDownLeft == true && collisionUpLeft == false))
                 {
                     collisionDirection = 7;
                 }
                 if((collisionUp == true && collisionUpLeft == true && collisionLeft == true) ||
                     (collisionLeft == true && collisionUpLeft == true && collisionDownLeft == false) || 
                     (collisionUp == true && collisionUpLeft == true && collisionUpRight == false))
                 {
                     collisionDirection = 8;
                 }
     
     
                     } else {
                 collisionDirection = 0;
                     }
             }


The next part is in the GO Movement-Script. This part compares the collisionDirection from my raycasting script and the direction the player walks at the moment.

 void CompareCollisionAndWalkDirection() {
 
         coldirection = raycasting.collisionDirection;
     
         if (coldirection > 0) {
             if (direction == coldirection) {
                     collisionInFront = true;
                 }
                 else {
                     collisionInFront = false;
                 }
         } 
         else {
             collisionInFront = false;
         }    
     }

And the last part is also in the Movement-Script. This part is very small and only sets the movement to stop the In-front is true.

     void checkCollisions()
     {
    
         if (collisionInFront == true) {
                         walkSpeed = 0f;
                 } else {
             walkSpeed = 5.0f        
         }
 
     }

Sooooo and now finally to my problem. The script work and the player stops at a wall... But my problem are edges. If I collide against a without a edge all works like I wish. But if there are an edge I cant set the collisionDirection in my raycasting script right.

So to be short: How can I write my "GetCollisionDirection" void so that it works than more the 1 collision is true. It have to work with 8 directions. My solution don't work very well, as you see :(

Okay I know my English is not the best, so I made small picture that describes my problem^^ The green Box is the Player and the yellow arrow is his movment direction. The red dots are his raycast colliders and the blue Line is the wall or something like that^^

alt text

I realy thank you for your time :)

prob.png (3.4 kB)
Comment
Add comment · Show 2
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 revolute · Oct 02, 2014 at 06:48 AM 0
Share

$$anonymous$$ay I ask why you wish to create your own collision system? Unity has 2D colliders that helps out a lot..

avatar image Darkkingdom · Oct 02, 2014 at 07:00 AM 0
Share

Ahhh sorry I forget to say that I use a Unity 2DBoxcollider for all my objects

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by revolute · Oct 02, 2014 at 07:44 AM

I understood where your code went wrong.

First of all, your picture is this in the code : collisionUp == true && collisionUpLeft == true && collisionUpRight == false

This unfortunately can occur when moving in all 2,5,8 directions. Collision is simply not limited to moving in direction 8.

However, there is an easy solution if you simply want objects to stop on collision.

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 Darkkingdom · Oct 02, 2014 at 07:57 AM 0
Share

Yeah thats right, you got my code^^ But a small question which solution do you mean?^^ The link is to the 2D Physics mainpage^^ What parts of the physics do I need? (I already use 2d-Boxcolliders and a Ridigidbody2D)

avatar image revolute · Oct 02, 2014 at 08:01 AM 0
Share

In that case, 2D collision manual will help.

avatar image revolute · Oct 02, 2014 at 08:02 AM 0
Share

When your objects with 2d colliders and 2d rigidbody actually collides, OnCollisionEnter2D is called and tells you what has collided. Parameter is the object that this object collided into.

avatar image Darkkingdom · Oct 02, 2014 at 09:25 AM 0
Share

Yeah I think OnCollisionEnter2D would be very helpful. But I have to compare my movement direction with my colliding Object, so that I can get, if the object is in front or not. I mean: If I only query the enter of a collision, my movement script will be always off from the first hit. And I cant move to the left or to the right to avoid my obstacle. So i have to compare it with my movement direction in someway^^

Do you have any ideas how to realize this with my 8-way?^^ (And thank you so much for your help :) )

avatar image revolute · Oct 02, 2014 at 09:39 AM 0
Share

You can get colliding points. Collision2D contains ContactPoints2D which contains Vector2 of collision point. Collision2D and ContactPoints2D and Vector2

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Custom Collision Detection 4 Answers

Collision Detection If Raycast Source Is inside A Collider? 4 Answers

Need help with third-person camera stuttering involving raycasts and clamp 0 Answers

AI create path, help. 1 Answer

Help with a field of sight 3 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