Could you please tell me how to change the “Y” position of a Raycast?, I need to detect small tiles on the flor but it’s a little high now, and I can’t find the way… this is an example:
Your code is probably correct to start the raycast at the transform.position, but the direction you give it is Vector2.right which will always be equal to Vector2(1,0)
If you want that vector to angle downwards, you’ll need to set it’s y value to a small negative. Something like this should work for you:
Physics2D.Raycast(transform.position, new Vector2(1, -1), Reach, _Default.value)
The vector (1, -1) will point diagonally down and to the right.
I’m not sure however how this is working for you at all, as a vector pointing right shouldn’t ever hit the yellow tile you’ve indicated, as that tile is on the negative x axis (left) of the transform. But maybe you’re handling that somehow as you haven’t said that’s a problem.
You’re welcome to reply here if this isn’t resolved and you’re still having trouble.
You also might want to consider a much better way to detect contacts. I’ve made many posts about it but you can find a recent one here including videos and demos in my physics examples GitHub repo.
sorry, my explanation was not so well detailed, I’m going to try to describe the problem a little better this time and the way that I’ve found to make it work, also, maybe you can tell me if it’s a good way too work around this.
what I need is to position the begining of the Rays (both left and right ones) a little LOWER to be able to detect the small tiles in front of the enemy (yes I know, in the code I wrote “right” direction and I was highlighting the left side) but that is because I’ve wrongly copied the same piece of code that I’m using for each side that is basically the same, only changing right to left.
So, I’m talking about the position of the Ray (transform.position) and I figured out by myself (in a very rustic way) how to make this, let me know what do you thing:
void FixedUpdate()
{
var pos_original = transform.position;
var pos_lowered = new Vector3(pos_original.x, pos_original.y - 0.06f);
if (Physics2D.Raycast(pos_lowered, Vector2.right, Rambon_Reach, _Default.value))
{
//For example, to jump
}
This is a very nice way, I’m going to try it! (now I’m using Rays).
I need to lower the ray a little because I’m designing an AI for the enemies to be able to jump when they get stucked.
Also note that all of 2D physics uses Vector2 not Vector3. Whilst Unity will make this easier for you, it can catch you out when doing things like calculating distances between positions. If they’re Vector3 then they’ll include the Z which might throw you off. You might want to stick to using Vector2 when interacting with the 2D stuff.
Also, when performing physics queries, know that the Rigidbody2D is the authority on position, not the Transform. They won’t be the same if you, for instance, use interpolation and again, subtle mistakes can creep in. Rigidbody2D writes to the Transform, not the other way around.
Sounds like you’re moving in the right direction and things are working for you now.
Personally I’d write the lowerd pos like this, I think it’s jsut a little more readable.
void FixedUpdate()
{
var offset = new Vector2(0, -0.06f);
if (Physics2D.Raycast(transform.position + offset, Vector2.right, Rambon_Reach, _Default.value))
{
//For example, to jump
}
I don’t see how that makes any difference unless the transform position was exactly at the ground level and even then it’s prone to precision errors with that magic value. Querying contacts is 100% stable and faster and doesn’t rely on the geometry of the colliders. Also, using something not so “thin” such as a CircleCast would be better in place of raycast but still not as good as just detecting contacts.
Obviously detecting contacts is only good if you want to know the current state, not the future state i.e. where you can move to.
Finally, I have to state that using Transform.position is always a bad idea, use Rigidbody2D.position as that is the authority on position, especially if you’re using interpolation/extrapolation as they won’t be the same.
Here’s the actual documentation page for Physics2D.Raycast(), if you get used to reading the docs they help a lot in understanding what you can do with… well everything in Unity.
There’s a page like this for everything in Unity, so it’s a super useful resource when you’re writing your scripts.