# How to convert this physics2d raycast with ^ code to physics raycast

While searching on the net I encountered a piece of code in a tutorial I want to use in my game but don't know how to convert it to physics.raycast (https://www.youtube.com/watch?v=1i1hTLU6JTY)

it's about the following piece of code I want to convert:

RaycastHit2D maxSlopeHitLeft = Physics2D.Raycast (raycastOrigins.bottomLeft, Vector2.down, Mathf.Abs (moveAmount.y) + skinWidth, collisionMask);
RaycastHit2D maxSlopeHitRight = Physics2D.Raycast (raycastOrigins.bottomRight, Vector2.down, Mathf.Abs (moveAmount.y) + skinWidth, collisionMask);
if (maxSlopeHitLeft ^ maxSlopeHitRight) {
SlideDownMaxSlope (maxSlopeHitLeft, ref moveAmount);
SlideDownMaxSlope (maxSlopeHitRight, ref moveAmount);
}

I especially don't understand the use of ^, isn't it the same as saying

if((!maxSlopeHitLeft && maxSlopeHitRight) || (maxSlopeHitLeft && !maxSlopeHitRight))


I hope someone can help me with the 1st piece of code.

I tried to convert the code myself as follow

RaycastHit maxSlopeHitLeft, maxSlopeHitRight;
if(Physics.Raycast (raycastOrigins.bottomLeft, Vector2.down, out maxSlopeHitLeft, Mathf.Abs (moveAmount.y) + skinWidth, collisionMask) ^ Physics.Raycast (raycastOrigins.bottomRight, Vector2.down, out maxSlopeHitRight, Mathf.Abs (moveAmount.y) + skinWidth, collisionMask)) {
SlideDownMaxSlope (maxSlopeHitLeft, ref moveAmount);
SlideDownMaxSlope (maxSlopeHitRight, ref moveAmount);
}


when my player reaches an edge of a platform it freezes all movement, while in the source tutorial it doesn't

Here is the SlideDownMaxSlope method, it check whether the angle is high enough to slide and calculates the horizontal moving amount

void SlideDownMaxSlope(RaycastHit Hit, ref Vector3 MoveAmount){
float SlopeAngle = Vector3.Angle (Hit.normal, Vector3.up);
if (SlopeAngle > MaxSlopeAngle) {
MoveAmount.x = Mathf.Sign (Hit.normal.x) * (Mathf.Abs (MoveAmount.y) - Hit.distance) / Mathf.Tan (SlopeAngle * Mathf.Deg2Rad);

CI.SlopeAngle = SlopeAngle;
CI.SlideDownSlope = true;
CI.SlopeNormal = Hit.normal;
}
}


plus in an update function of another script there is a line for calculating the vertical moving amount

if(Grounded) {
if (PCS.CI.SlideDownSlope) {
Velocity.y += PCS.CI.SlopeNormal.y * -Gravity * Time.deltaTime;
} else {
Velocity.y = 0;
}
}

• Your interpretation of the ^ Exclusive Or (XOR) is correct. What problem are you having using this code or converting it to use 3D raycasts? Nov 3, 2017 at 17:00
• @DMGregory double check, that interpretation is wrong because the two expressions are the same Nov 3, 2017 at 17:08
• i tried to convert the code as follow RaycastHit maxSlopeHitLeft, maxSlopeHitRight; if(Physics.Raycast (raycastOrigins.bottomLeft, Vector2.down, out maxSlopeHitLeft, Mathf.Abs (moveAmount.y) + skinWidth, collisionMask) ^ Physics.Raycast (raycastOrigins.bottomRight, Vector2.down, out maxSlopeHitRight, Mathf.Abs (moveAmount.y) + skinWidth, collisionMask)) { SlideDownMaxSlope (maxSlopeHitLeft, ref moveAmount); SlideDownMaxSlope (maxSlopeHitRight, ref moveAmount); } when my player reaches an edge of a platform it freezes all movement, while in the source tutorial it doesn't
– Ken
Nov 3, 2017 at 17:12
– Ken
Nov 3, 2017 at 17:30
• I'm sorry but what game are we talking about? 2.5d? What are the degrees of freedom? Converting from 2d to 3d is not just code, can you explain the situation a little bit better? Nov 3, 2017 at 17:41

Before adding the line if(Hit.collider != null) { player will freeze all movement when reaching an edge. Because there are 2 raycasthit involved in the process of detecting slopes, I added an if statement to filter out the 1 raycast that actually does hit a slope.

RaycastHit maxSlopeHitLeft, maxSlopeHitRight;
if(Physics.Raycast (raycastOrigins.bottomLeft, Vector2.down, out maxSlopeHitLeft, Mathf.Abs (moveAmount.y) + skinWidth, collisionMask) ^ Physics.Raycast (raycastOrigins.bottomRight, Vector2.down, out maxSlopeHitRight, Mathf.Abs (moveAmount.y) + skinWidth, collisionMask)) {
SlideDownMaxSlope (maxSlopeHitLeft, ref moveAmount);
SlideDownMaxSlope (maxSlopeHitRight, ref moveAmount);
}

void SlideDownMaxSlope(RaycastHit Hit, ref Vector3 MoveAmount){
if(Hit.collider != null) {
float SlopeAngle = Vector3.Angle (Hit.normal, Vector3.up);
if (SlopeAngle > MaxSlopeAngle) {
MoveAmount.x = Mathf.Sign (Hit.normal.x) * (Mathf.Abs (MoveAmount.y) - Hit.distance) / Mathf.Tan (SlopeAngle * Mathf.Deg2Rad);

CI.SlopeAngle = SlopeAngle;
CI.SlideDownSlope = true;
CI.SlopeNormal = Hit.normal;
}
}
}

• This answer would be even better if it included a little description about what you changed and why that fixed the problem. That way other users with a similar problem that's not exactly the same can learn from your problem-solving process. Nov 6, 2017 at 15:22