- Home /
Question by
stanferrolino12 · Oct 14, 2020 at 05:26 AM ·
rigidbodycharactercontrollerdouble jump
Any tips to fix and improve this movement script?
I have a movement script that I created which is supposed to have double jumping and diving. I have experience in scripting but I am new to unity (The original script was ripped off from a Brackeys tutorial), so my script was very sloppy. This gave way to many bugs in the script (infinite double jumps, movement doesn't stop after moving, etc), which I can't fix, because I really dont understand much about the movement system. Any help on how to fix it?
public class Movement : MonoBehaviour
{
Rigidbody Rb;
Collider Collider;
public GameObject Cameraboi;
public float Speed;
public float jumpheight;
public float FaceDamping;
public bool DoubleJump;
public float DoubleJumpHeight;
public bool Dive;
bool Grounded;
public LayerMask Jumpable;
float AngleVelocity;
[Range(0, 10)]
public float GroundCheckHeight;
[Range(0, 10)]
public float GroundCheckRadius;
bool Jumped;
bool CanDoubleJump;
bool DoubleJumped;
bool CanDive;
bool Comboed;
public GameObject LandJump;
public GameObject AirJump;
public GameObject AirDive;
public GameObject RunParticles;
// Start is called before the first frame update
void Start()
{
Rb = GetComponent<Rigidbody>();
Collider = GetComponent<Collider>();
Cameraboi = Camera.main.gameObject;
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
Grounded = CheckGround();
float Xaxis = Input.GetAxisRaw("Horizontal");
float Zaxis = Input.GetAxisRaw("Vertical");
Vector3 GoalVelocity = new Vector3(Xaxis, 0, Zaxis);
if(GoalVelocity.magnitude > 0.5)
{
float TargetAngle = Mathf.Atan2(GoalVelocity.x, GoalVelocity.z) * Mathf.Rad2Deg + Cameraboi.transform.eulerAngles.y;
float SmoothAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, TargetAngle, ref AngleVelocity, FaceDamping);
transform.rotation = Quaternion.Euler(0f, SmoothAngle, 0);
Vector3 RealVelocity = Quaternion.Euler(0f, TargetAngle, 0) * Vector3.forward;
Rb.velocity = new Vector3(RealVelocity.x * Speed, Rb.velocity.y, RealVelocity.z * Speed);
}
else if(Grounded)
{
Rb.velocity = new Vector3(0, Rb.velocity.y, 0);
}
if (Grounded && GoalVelocity.magnitude > 0.5)
{
if(!RunParticles.GetComponent<ParticleSystem>().isPlaying)
{
RunParticles.GetComponent<ParticleSystem>().Play();
}
}
else
{
RunParticles.GetComponent<ParticleSystem>().Stop();
}
if(Grounded && Input.GetButtonDown("Jump") == true)
{
Rb.AddForce(new Vector3(0, jumpheight),ForceMode.Impulse);
LandJump.GetComponent<ParticleSystem>().Play();
Jumped = true;
}
if (DoubleJump && Input.GetButtonDown("Jump") == true)
{
if (Comboed == true)
{
CanDoubleJump = false;
}
if (CanDoubleJump == true)
{
Rb.AddForce(new Vector3(0, DoubleJumpHeight), ForceMode.Impulse);
AirJump.GetComponent<AudioSource>().Play();
CanDoubleJump = false;
DoubleJumped = true;
Jumped = false;
}
}
if(Dive && Input.GetButtonDown("Jump") == true)
{
if(Comboed == true)
{
CanDive = false;
}
if(CanDive == true)
{
if (GoalVelocity.magnitude < 0.5)
{
Rb.AddRelativeForce(new Vector3(0, 1f, 20f), ForceMode.Impulse);
AirDive.GetComponent<AudioSource>().Play();
AirDive.GetComponent<ParticleSystem>().Play();
Comboed = true;
CanDive = false;
DoubleJumped = false;
Jumped = false;
Comboed = true;
}
}
}
if (Grounded == false && Comboed == false)
{
Invoke("SetCanDoubleJump", 0.3f);
Debug.Log("setcandouble");
}
if(DoubleJumped == true && Comboed == false)
{
Invoke("SetCanDive", 0.2f);
Debug.Log("setcandove");
}
if(Grounded && !Jumped)
{
Jumped = false;
CanDoubleJump = false;
CanDive = false;
DoubleJumped = false;
Comboed = false;
}
}
bool CheckGround()
{
Collider[] Hits = Physics.OverlapSphere(new Vector3(transform.position.x, transform.position.y - GroundCheckHeight, transform.position.z), GroundCheckRadius, Jumpable);
bool Outcome = false;
if(Hits.Length > 0)
{
Outcome = true;
}
return Outcome;
}
void SetCanDoubleJump()
{
CanDoubleJump = true;
}
void SetCanDive()
{
CanDive = true;
}
}
Comment