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;
}
}
^
Exclusive Or (XOR) is correct. What problem are you having using this code or converting it to use 3D raycasts? \$\endgroup\$