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 Frostbite23 · Jan 01, 2014 at 01:40 AM · 2djavascriptraycast

Need Help Converting 3d Raycast Into a 2D Raycast

Ok, I have been needing a lot of help especially sense Unity 4.3 came out, and sense that happened I need help converting my 3d ray cast into a 2d ray cast, I've checked out the scripting reference but however its not telling me how, unity is still complaining that I vector3 variables, how can i fix this http://pastebin.com/dKgKQgjv

         var trans : Vector2 = new Vector2(transform.position.x, transform.position.y);
         var hit : RaycastHit2D;
         var fwd = transform.TransformDirection(Vector2.right);
         
         Debug.DrawRay(trans,fwd);
         
         if(Physics2D.Raycast(trans,fwd,hit,range)){

and than Unity Recives this "No appropriate version of 'UnityEngine.Physics2D.Raycast' for the argument list '(UnityEngine.Vector2, UnityEngine.Vector3, UnityEngine.RaycastHit2D, int)' was found."

Im still receiving errors and i assume its with the fwd line.

Comment
Add comment · Show 4
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 Frostbite23 · Jan 01, 2014 at 01:52 AM 0
Share

dammit it keeps getting messed up

avatar image HappyMoo · Jan 01, 2014 at 02:05 AM 0
Share

you can select your code and then press the code sample button

avatar image Frostbite23 · Jan 01, 2014 at 02:12 AM 0
Share

also if i may add, I tried adding the code, but after posting it looks like an absolute mess

avatar image robertbu · Jan 01, 2014 at 02:14 AM 0
Share

@Frostbite23 - Past your code, select your code, then click on the 101/010 button.

2 Replies

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

Answer by HappyMoo · Jan 01, 2014 at 02:10 AM

You can't use transform.position as this is an Vector3.

You need to convert it to Vector2 somehow - this of course depends on how your scene is setup, maybe like this:

 Vector2 trans = new Vector2(transform.position.x, transform.position.y);
Comment
Add comment · Show 8 · 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 Frostbite23 · Jan 01, 2014 at 02:12 AM 0
Share

aye aye aye, thank you Happy$$anonymous$$oo and how exactly can i use this in my code? do i just replace transform with "trans"?

avatar image HappyMoo · Jan 01, 2014 at 02:15 AM 0
Share

You replace transform.position with trans...

avatar image Frostbite23 · Jan 01, 2014 at 02:29 AM 0
Share

ok 1 more thing, Ive Implemented this just now and unity receives an error ":" expected, insert a semicolon in the end, i assume i did not put it correctly in the code. heres what i have http://pastebin.com/LLB2XZax

avatar image HappyMoo · Jan 01, 2014 at 02:35 AM 0
Share

I've sent you C# code, you use javascript.

replace this:

 Vector2 trans

with this:

 trans : Vector2
avatar image Frostbite23 · Jan 01, 2014 at 04:12 AM 0
Share

thanks but I'm still reciving errors, i edited the post so you can fix and see what errors there are

Show more comments
avatar image
0

Answer by segana · Jan 09, 2014 at 01:08 PM

Hi Frostbite23,

I believe your issue is you're trying to use Physics2D.Raycast the same way as Physics.Raycast.

In your last line: if(Physics2D.Raycast(trans,fwd,hit,range)){

You need to remove 'hit'.

The new Physics2D only accepts 'origin, direction, distance, layerMask, minDepth & maxDepth'

Matt

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

20 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

Related Questions

Create clickable GameObjects (JS) 1 Answer

GetComponent() problems 2 Answers

Change gravity on button 1 Answer

Switch GameObjects Tags with javascript 1 Answer

2D: Rotating to face mouse position with two fixed axes 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