Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 S_Byrnes · Jul 31, 2015 at 12:58 AM · c#2dtransformplayer

Please Help - 2D Collisions Without Unity's Physics

Hey guys, I'm really stuck, I've been trying to solve this myself for a month now with no luck, I really need your help.

I'm trying to have a player fly through obstacles that are moving towards him (he's stationary), and I have pathways in the obstacles that he's allowed to pass through.

What I need is for him to be able to collide with the top and bottom of those pathways and stop from going further 'inside' the obstacles walls.

Also, I'll need him to die if he doesn't make it into the pathways and instead hits the wall first, but I can probably solve that part myself once I have the path collisions worked out.

Here is what I've tried so far, note the comments, it's the one down the very bottom that needs fixing:

 void OnTriggerEnter2D(Collider2D obstacles) {
         
         if (obstacles.gameObject.tag == "Obstacles") {
             playerInObstacle = true;
         }
         
         if (obstacles.gameObject.tag == "Path") {
             playerInPath = true;
             path = obstacles;
         }
     }
     
     void OnTriggerExit2D(Collider2D obstacles) {
         
         if (obstacles.gameObject.tag == "Obstacles") {
             playerInObstacle = false;
         }
         if (obstacles.gameObject.tag == "Path") {
             playerInPath = false;
         }
     }
     
     void Update() {
         
         //a bunch of other movement stuff is actually in this section, if you need that, let me know and I'll post it.
         
         if (ySpeed > ySpeedLimit) {
             ySpeed = ySpeedLimit;
         } else if (ySpeed < -ySpeedLimit) {
             ySpeed = -ySpeedLimit;
         }
         
         transform.position += Vector3.up * ySpeed * Time.deltaTime;
         
         //floor collisions (this one works)
         
         if (playerY < Y_Min) {
             floorOn = true;
             ySpeed = noSpeed;
         } else {
             floorOn = false;
         }
         
         //path & obstacles collisions (this one doesn't work)
         
         if (playerInObstacle == true) {
             
             if (playerInPath == true) {
                 
                 if (playerY > path.bounds.max.y) {
                     ySpeed = noSpeed;
                 } else if (playerY < path.bounds.min.y) {
                     ySpeed = noSpeed;
                 }
             }
         }
         
     }

Thank you very much in advanced, I really appreciate the help!

Comment
Add comment · Show 3
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 Bugs bunny · Jul 31, 2015 at 06:37 AM 0
Share

use raycast ..its good. example

vetor2 direction = rightedge.position -centerpoint.position;// direction = direction.normalized;//gets the direction of ray. // Debug.DrawLine (rightedge.position, centerpoint.position, Color.green); RaycastHit2D RH = Physics2D.Raycast (centerpoint.position, direction, 15f); if(RH.collider != null) { RH.collider.gameObject.setActive(false); //whatever blah blah } if u need more idea,pls let me know....

avatar image Andres-Fernandez · Jul 31, 2015 at 06:44 AM 0
Share

There was a post on the forums or UA about this very same subject a few months ago, but I can't find it.

You can use Unity physics to achieve that. Just use non-trigger colliders and a rigidbody on the player (just watch out for the functions you use to move it). If the player hits the obstacle from top or bottom, it'll stop moving. If it hits the obstacle from a direction other than top or bottom, it will be pushed backwards (towards a killing collider). No need for OntriggerEnter functions. Just create the obstacles as separate objects ins$$anonymous$$d of objects with holes.

[Edit] Using colliders and OnTriggerEnter/Exit functions already is using Unity's physics.

[Edit 2] I couldn't find the post, but this live session will do. It $$anonymous$$ches the physics of the obstacles.

avatar image S_Byrnes · Jul 31, 2015 at 11:50 AM 0
Share

The first edit you put - But I've used colliders for the floor and used it as triggers and then changed the value of ySpeed and it worked... Wouldn't the same thing work in this situation?

Second edit - Alright I'll check that out, still thinking it should be possible with the direction I was heading though..

@ Bugs Bunny - Thanks, I'm not too sure if what you posted will work or not, I'll have to see if properly formatted, so I'll go try it out. But is that for the up and down on the y direction only?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ArgoEclipse · Jul 31, 2015 at 06:34 AM

Why not just add a rigid body to the game objects?

Comment
Add comment · Show 1 · 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 S_Byrnes · Jul 31, 2015 at 06:36 AM 0
Share

I've tried that, but when the player touches the collider, it does a kind of jerking motion. Not a smooth sail across it like I need.

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

25 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

Related Questions

Changing player's moving direction 0 Answers

How can I make a collision sensitive teleporter for a 2D game #c 1 Answer

Need your help by movement 0 Answers

2D Character Switching HELP! 0 Answers

How do you make a player be still? 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