I have a player with two raycast and when they are hitting an object with tag “floor”, you can jump. But when the player is on a platform whose scale is being changed every frame he doesn’t jump, and he makes the animation of falling, I don’t know what’s happening.
Here is the complete code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public Rigidbody2D rigid;
public float speed; // The speed of horizontal axis
public float jumpForce; // The power of the jump
public bool CanIJump; // I don't use it in the code, I think that it was something that I tried in the past
public RaycastHit2D myRay; // The Raycast on the right and under the player
public RaycastHit2D myRay2; // The Raycast on the left and under the player
public float distance; // The lenght of the raycast under the player
private Vector2 rayDown; // The same that CanIJump, but I don't want to put off
private Movement velocidad; // The same...
[SerializeField] LayerMask floor; // The layer that the raycasts must identify for jump
public float TimeInTheVoid; // When the raycast are not touching the floor is, Time.DeltaTime is being added
public float CoyoteTime; // If you press UpArrow to jump and the raycasts are not touching the floor, if TimeInTheVoid<Coyotetime, you jump
private Vector2 MyRayPos; // It's the point where the Raycast on the right is being shot
private Vector2 MyRayPosReference; // It's the same as the position of the player
private Vector2 MyRayPosMenos; // It's the vector2 which rest to the MyRayPosReference
private Vector2 MyRayPos2; // It's the point where the Raycast on the left if being shot
private Vector2 MyRayPosMas; // It's the vector2 which rest to the MyRayPosReference
public RaycastHit2D myRayUL; // It's the raycast on the left on the player
public RaycastHit2D myRayUR; // It's the raycast on the right on the player
public float distance2; // The lenght of the myRayUL and myRayUR
public float jumpHelp; // The distance that is added or rested depending on, if one the raycast on the top is true and the other not
public bool noDoubleJump; // A condition to jump, it's like a cooldown
public float TimeToJumpAgain; // The time to noDoubleJump to be true again
public Vector3 initialPosition;
public AudioSource audioSourcee;
public AudioClip Jump;
// Start is called before the first frame update
void Start()
{
audioSourcee = GetComponent<AudioSource>();
transform.position = initialPosition;
noDoubleJump = true;
rigid = GetComponent<Rigidbody2D>();
TimeInTheVoid = 0f;
MyRayPosMenos = new Vector2(-0.279f, 0);
MyRayPosMas = new Vector2(0.5f, 0);
}
// Update is called once per frame
void Update()
{
// All this is for have the point of shot of the raycasts from the player
MyRayPosReference = gameObject.transform.position;
MyRayPos = MyRayPosReference-=MyRayPosMenos;
MyRayPos2 = MyRayPosReference -= MyRayPosMas;
myRay = Physics2D.Raycast(MyRayPos, Vector2.down, distance,floor);
Debug.DrawRay(MyRayPos,Vector2.down*distance);
myRay2 = Physics2D.Raycast(MyRayPos2, Vector2.down, distance, floor);
Debug.DrawRay(MyRayPos2, Vector2.down * distance);
myRayUL = Physics2D.Raycast(transform.position + new Vector3(-0.213f, 0.370f), Vector2.up, distance2, floor);
myRayUR = Physics2D.Raycast(transform.position + new Vector3(0.279f, 0.370f), Vector2.up, distance2, floor);
Debug.DrawRay(transform.position + new Vector3(-0.213f, 0.370f), Vector2.up * distance2);
Debug.DrawRay(transform.position + new Vector3(0.279f, 0.370f), Vector2.up * distance2);
if (Input.GetKey(KeyCode.RightArrow))
{
rigid.AddForce(Vector2.right * speed * Time.deltaTime, ForceMode2D.Force);
if (myRay==true)
{
GetComponent<Animator>().SetBool("Run", true);
GetComponent<SpriteRenderer>().flipX = false;
}
else
{
GetComponent<Animator>().SetBool("Run", false);
GetComponent<SpriteRenderer>().flipX = false;
}
}
if (Input.GetKey(KeyCode.LeftArrow))
{
rigid.AddForce(Vector2.left * speed*Time.deltaTime, ForceMode2D.Force);
if (myRay == true)
{
GetComponent<Animator>().SetBool("Run", true);
GetComponent<SpriteRenderer>().flipX = true;
}
else
{
GetComponent<Animator>().SetBool("Run", false);
GetComponent<SpriteRenderer>().flipX = true;
}
}
if (!Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.RightArrow))
{
GetComponent<Animator>().SetBool("Run", false);
}
// HERE IS THE JUMP SYSTEM
if (Input.GetKeyDown(KeyCode.UpArrow) && noDoubleJump == true)
{
noDoubleJump = false;
Invoke("noDoubleJumpDelay", TimeToJumpAgain);
JumpSystem();
}
if (myRay==false && myRay2==false)
{
GetComponent<Animator>().SetBool("Jump", true);
TimeInTheVoid += Time.deltaTime;
}
if (myRay == true || myRay2==true)
{
GetComponent<Animator>().SetBool("Jump", false);
TimeInTheVoid = 0;
}
if (myRayUL==true && myRayUR==false)
{
transform.position += new Vector3(jumpHelp, 0, 0);
}
else if (myRayUR==true && myRayUL==false)
{
transform.position -= new Vector3(jumpHelp, 0, 0);
}
}
void noDoubleJumpDelay()
{
noDoubleJump = true;
}
void JumpSystem()
{
if (myRay == true)
{
rigid.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
audioSourcee.clip = Jump;
audioSourcee.volume = 0.2f;
audioSourcee.Play();
}
else
{
if (TimeInTheVoid < CoyoteTime)
{
rigid.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
audioSourcee.clip = Jump;
audioSourcee.volume = 0.2f;
audioSourcee.Play();
}
}
}
private void FixedUpdate()
{
}
}