Beginner Trouble with Vector2/3

Hi,

I am currently trying to walk through a tutorial series on youtube demonstrating how to create a platformer with raycast detection (video link here:

).

However I run into a problem on line 59:

Vector2 rayOrigin = (directionY == -1)?raycastOrigins.bottomLeft:raycastOrigins.topLeft;

and receive an error stating:

Type of conditional expression cannot be determined because ‘UnityEngine.Vector’ and ‘UnityEngine.Vector2’ implicitly convert to one another (CS0172).

I’m hardly even a beginner when it comes to programming, but the funny thing is I managed to replicate this project/script a couple months ago and still have it saved (and it works just fine).

So to clarify, I have a problem with a script that I had no problem with before (and still have and can run it), but I want to know/learn how I can fix the current problem.

Sorry if this post’s format is confusing.

Thank you all for any help and your time.

I glanced through the source code (link to the Github below for anyone else), but I can’t actually determine why it is throwing an error. Is there somewhere I can download the completed project?

https://github.com/SebLague/2DPlatformer-Tutorial

I just looked through all videos and the Github page and sadly I don’t think there is a completed project download.

But thanks for your reply, makes me feel a little less crazy, haha.

I’m stumped as well - error doesn’t make any sense in this context.

Try expanding the code to this:

Vector2 rayOrigin;

if(directionX == -1)
    rayOrigin = raycastOrigins.bottomLeft;
else
   rayOrigin = raycastOrigins.bottomRight;

All right, I expanded it as suggested by GroZZleR, and now the code runs successfully builds without error.

… But it doesn’t function as it should, as in the player object is not colliding/detecting the obstacle.

But, that’s just fine. I’m just very relieved I wasn’t overlooking something simple or what not. I’ll just start over from scratch and maybe post here if I run into some more problems.

Thanks very much for your help, Ryiah and GroZZleR. You guys helped a lot more than you may believe.

I generally suggest avoiding the ternary operator for all but the most simple lines. It’s confusing to keep track of the logic. That said, I’m puzzled why it didn’t work.

I have encountered similar errors in the past due to the nature of implicit casting between Vector3 and Vector2. This line

myVector3 + myVector2

will throw a compiler error because the compiler can’t figure out if I meant Vector3 addition or Vector2 addition. It might be something similar.

Ah, all right. Thanks for your input as well, BoredMormon. Its a relief to know vastly more competent people than myself are also puzzled at this bizarre occurrence.

Yeah, the implicit cast is annoying. By proper programming rules a cast from a Vector3 to a Vector2 should be explicit, as you are losing information. It’s one of Unity’s ‘shortcuts’ that causes hassles.

Try:

myVector3 + (Vector3)myVector2
1 Like

I know the answer. Just throwing it up as a possible reason for the op. But thanks.

Without the double implicit cast the line would work fine.

1 Like

I literally just discovered you could simply cast a vector2 to a vector3 yesterday. ;). I was working on some ui stuff and was doing it the long way (new vector2(v1.x,v1.y,0; ). One the other guys asked why I wasn’t just casting it. And then learning happened! :wink:

2 Likes