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 CrimsonChin · Jul 22, 2015 at 04:55 AM · 2draycastaxis

Ray Casting in 2D

I have been implementing a tutorial I found on youtube. Its a simple 2D game and allows you to move a sprite around the screen as follows:

     Vector2 movementVector = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
     if (movementVector != Vector2.zero)
     {
         _anim.SetFloat("inputX", movementVector.x);
         _anim.SetFloat("inputY", movementVector.y);
     }

Now I'm trying to introduce ray casting, and I think my Z index is causing me problems. I don't rotate on z axis at any point, so when I try to ray cast it points "up" (away from the screen in the 3d view).

How do I make it point forward (in the direction the sprite is facing) on a 2D plain? I get the impression this gets asked a lot but I'm having trouble implementing a fix.

I'm using the unity docs but I've hit a road block. This is the sort of code I'm aiming for:

     Physics2D.Raycast(transform.position, transform.forward, 10f);
     Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
     Debug.DrawRay(transform.position, forward, Color.green);

Here's the tutorial I'm using for full reference: https://www.youtube.com/watch?v=XZDjkQ8wEd0

Comment
Add comment · Show 1
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 sas_88 · Jul 22, 2015 at 08:22 AM 0
Share

Check with this video.

https://www.youtube.com/watch?v=-mtzOvdhsuo

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Hellium · Jul 21, 2015 at 08:42 PM

Since you are in 2D you want the right vector instead of the forward vector.

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 Cherno · Jul 22, 2015 at 06:45 AM 0
Share

Converted to answer since this is true. For side-scrolling, use (-)transform.right. For top-down, use (-)transform.up. Transform.forward is positive z, so it goes into the background.

avatar image CrimsonChin · Jul 22, 2015 at 07:44 AM 0
Share

transform.up was a good call! Thanks.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I raycast the direction my 2D character is facing? 1 Answer

Layer Mask on raycast2d not working? 1 Answer

Raycast for a 2D objects 0 Answers

2D sidescroller shooting 1 Answer

Finding any objects below or above object at ANY POINT? 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