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 BTStone · Oct 22, 2012 at 08:59 PM · 2dmovementraycastgrounded

Using Raycasts in order to move around

Hello there,

the last days i tried to move my 2D character around with Rigidbodies + Box/Spherecolliders and CharacterController. I wasn't really happy with them, cause they made me a lot of problems.

Now i thought about using Raycasts, but i kind of don't know exactly how to start. I read the ScriptReference and tried something like this out:

 void IsGrounded()
     {
         if(Physics.Raycast(transform.position, Vray_down, out ray_hit, 20))
         {
             if(ray_hit.collider)
             {
                 Debug.Log("YOU HIT THE FLOOR!");
             }
         }
     }

i tried it by Moving a Sprite-Object in the Scene-Editor around (while the game is playing) and if it is at the Top of the floor, i get this message. Everything is fine. But, now, HOW exactly can i apply my Object Gravity and tell it to stay on the floor, when the Raycast hits the collider of the floor?

The second thing is: Where exactly starts the RayCast? In the Center of the Gameobject? It would be much easier to handle it, when it would start at the bottom of the Sprite (the feet), can i change that (eventually by script?)

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

Answer by spiceboy9994 · Oct 24, 2012 at 12:42 AM

Hey:

You could add gravity to you player with a rigidbody on it. Just be sure to add a collider to both your player and the floor, so the player stays over it. Another thing that you should keep in mind is to add a tag or a layer to your floor object so you can check if the player is actually colliding with your floor. Something like if (ray_hit.gameObject.tag == "floor").

For your second question, yes by default the RayCast starts from your object pivot point. There's a script that allows to change the pivot point from some objects, but it not works with all kind of objects. If your player is just a plane, you will be fine, but if you are using a framework like 2D toolkit, you will have troubles to change the pivot. I have a great tip: If your player have a Collider attached, you could use something like transform.collider.bounds.min to get the values of the min point of your collider. Then you could start your raycast from this point or from a new Vector3 that uses some of this coordinates.

I've attached the SetPivot.txt (just change the ext to cs)link text Script. Just be sure to put this script inside the Editor folder within your solution Folder. (if the folder does not exist, you have to create it)

Hope that helps you.


setpivot.txt (4.8 kB)
Comment
Add comment · 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
0

Answer by BTStone · Oct 24, 2012 at 11:41 AM

Thank you for the comment, but it isn't really what i was looking for. As mentioned in the first post, i don't want to use rigidbodies or colliders.

But thanks for the tip with the PivotPoint!

Comment
Add comment · 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

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

Cannot move elements in 2D scene. Help 3 Answers

Moving Player Up and Down in 2D 1 Answer

2D rigidbody movement & walls 2 Answers

Random Movement in 2D 2 Answers

Detect touched 2d object in Unity3d 4.3 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