- Home /
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.
you can select your code and then press the code sample button
also if i may add, I tried adding the code, but after posting it looks like an absolute mess
@Frostbite23 - Past your code, select your code, then click on the 101/010 button.
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);
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"?
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
I've sent you C# code, you use javascript.
replace this:
Vector2 trans
with this:
trans : Vector2
thanks but I'm still reciving errors, i edited the post so you can fix and see what errors there are
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
Your answer
Follow this Question
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