Hi all,
I’m making a super smash bros clone(won’t have the same characters), and it’s going fine. I had to edit my movement script a bit recently since it had alot of unneeded things, but now when I jump and move left or right the rigidbody floats. Here’s a webplayer to show how. Just go into training, jump (press P) and move left or right (A or D). or just see how slow it falls back after jumping in general. It didn’t do this with the last script, but I can’t use the last one since I want jumping to be delayed.
https://dl.dropboxusercontent.com/u/85846965/SUB/SUB.html
I want jumping to have a small delay between when you press jump and it actually does it (already implemented), so UP+P attacks can work fine.
Any help?
Here’s my code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
//Movement
private Rigidbody _Rig; //Reference to the current rigidbody
private bool CanJump = true; //If you can jump
private float Speed = 5; //The speed we are currently going
private float JumpSpeed = 10; //The speed of the jump we currently have
[HideInInspector]
public bool Moving = false;
//[HideInInspector]
public bool Grounded = true;
//[HideInInspector]
public int CJump = 0;
[HideInInspector]
public bool Jump = true;
public bool Dashing = false; //Bool for if we're dashing
public float Jumps = 1; //How many jumps you have
public float WSpeed = 5; //Walking speed
public float RSpeed = 8; //Running speed
public float JSpeed = 10; //Jump velocity
public float SJSpeed = 15; //Velocity of all jumps after the first
public float RayDist = 5; //Distance of ray to check Grounded
//General
public int Player = 1; //The player's number, used for controller
public float DecelSpeed = 0.9f; //Deceleration speed
[HideInInspector]
public bool Attk = false; //Checks if your attacking or not
//Multi-Jump
private bool CJumped = false; //If we have checked the last jump on the ground for jump delay
public float JumpDelay = 0.75f; //Delay between touching the ground and being able to jump again
public float JumpBDelay = 0.05f; //Delay before the jump to allow Up smashes
void Update () {
Jump = !Grounded; //Jump will never be true when your grounded
}
public void InputMan(float Hoz, float Vert, bool Jump){
Grounded = Physics.Raycast (transform.position+new Vector3(0, 0.5f, 0), -transform.up, RayDist);
//Limit Speed
if(rigidbody.velocity.magnitude > WSpeed){
rigidbody.velocity = rigidbody.velocity.normalized * WSpeed;
}
//Check Speed
if (!Dashing) {
Speed = WSpeed;
} else {
Speed = RSpeed;
}
//Movement
if (Moving && Attk == false) {
if(Vert < 0){
Vert = 0;
}
if(Grounded){
Vector3 temp = new Vector3(Hoz*WSpeed, 0, 0);
rigidbody.AddForce (temp, ForceMode.Impulse);
}else{
Vector3 temp = new Vector3(Hoz*WSpeed/1.5f, 0, 0);
rigidbody.AddForce (temp, ForceMode.Impulse);
}
}
//Checking if your grounded and it hasn't checked your jumps
if (Grounded && CJumped == false) {
Jump = false;
StartCoroutine("JumpD");
CJumped = true;
}
//First Jump
if (Grounded && Jump && CanJump && Attk == false && CJump < Jumps) {
Invoke("BJumpDelay", JumpBDelay);
}
//All Jumps After
if (CJump > 0 && Jump && CanJump && Attk == false && CJump < Jumps) {
Debug.Log("Double" + CJump.ToString());
Jump = true;
if(CJump == 0){
JumpSpeed = JSpeed;
}else{
JumpSpeed = SJSpeed;
}
rigidbody.AddForce (transform.up*JumpSpeed, ForceMode.VelocityChange);
CJump += 1;
CJumped = false;
}
Decelerate (Hoz);
LookDir ();
}
void BJumpDelay(){
//Delay before you jump
if(Attk == false){
Jump = true;
if(CJump == 0){
JumpSpeed = JSpeed;
}else{
JumpSpeed = SJSpeed;
}
rigidbody.AddForce (transform.up*JumpSpeed, ForceMode.VelocityChange);
CJump = 1;
CJumped = false;
}
}
void LookDir(){
//Look in direction of movement
if (Moving) {
Vector3 tempT = rigidbody.velocity;
tempT.z = tempT.x;
tempT.y = 0;
tempT.x = 0;
transform.rotation = Quaternion.LookRotation(tempT);
}
}
void Decelerate(float Hoz){
//Checks if moving, and also decelerates once you stop moving to prevent sliding
if (Hoz > 0 && !Attk || Hoz < 0 && !Attk) {
Moving = true;
} else {
Moving = false;
if(Grounded){
rigidbody.AddForce(rigidbody.velocity.x*(-transform.right*DecelSpeed));
}
}
}
IEnumerator JumpD(){
//Delay of jump after you hit the ground
CanJump = false;
yield return new WaitForSeconds(JumpDelay);
CanJump = true;
CJump = 0;
}
IEnumerator JumpAnimD(){
//Animation data for jumping
Jump = true;
yield return new WaitForSeconds(0.75f);
Jump = false;
}
}