- Home /
Question by
Bombshell93 · Jul 26, 2012 at 08:34 PM ·
animationmovementplayerjumpdouble jump
why will my jump animation only play on multi-jumps?
I have a script dedicated to the players movement, which recently I added the animation changing to, it seems to work fine bar 1 thing, when the player jumps, the jump animation plays for less than a second before reverting to whatever animation they had while on the ground, but the animation plays as expected on double / multi jump.
Here is my script, commented how I saw fit, hopefully anyone helping will be able to stick the pieces together while reading it, if anything isnt clear.
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class PlayerControlScript : MonoBehaviour {
[Serializable]
public class MoveInformation //Move information to keep the settings friendly
{
public float Acceleration = 1;
public float Deceleration = 1;
public float MaxSpeed = 10;
}
Vector3 previousDirection; //private Move associated variables
bool onGround = false;
float Speed = 0;
[Serializable]
public class JumpInformation //Jump information to keep the settings friendly
{
public int Limit = 2;
public float Delay = 4;
public float Speed = 20;
}
int hasJumped = 100; //private Jump associated variables
float jumpTimer = 0;
public float slopeLimit = 0.4f; //to what degree does a surface coutn as the ground
public JumpInformation jump;
public MoveInformation move;
public PlayerCameraScript.ViewInformation view;
PlayerCameraScript playerCamera; //player Camera control helper
void Awake()
{
previousDirection = transform.eulerAngles;
GameObject temp = Instantiate(Resources.Load("CameraObject")) as GameObject; //create an object to host the Camera control helper
playerCamera = temp.GetComponent(typeof(PlayerCameraScript)) as PlayerCameraScript;
playerCamera.InitiateCamera(transform);
playerCamera.view = view;
}
void Move(Vector3 temp) //Handles movement to keep code readable
{
Vector3 velocity = rigidbody.velocity;
if (onGround) //if on ground allow change in velocity
{
velocity.x = temp.x;
velocity.z = temp.z;
}
if (jumpTimer > 0) //if jump is delayed count down the timer
{
jumpTimer -= Time.deltaTime * 10;
}
else if (Input.GetAxis("Jump") != 0 && hasJumped < jump.Limit) //if player can jump
{
hasJumped++; //add jump interval
jumpTimer = jump.Delay; //delay next jump
velocity.y = jump.Speed; //set the speed
velocity.x = temp.x;
velocity.z = temp.z;
animation.CrossFade("jump"); //set the animation
animation.Play(); //make sure it plays every time a jump, doublejump or multi-jump takes place.
}
rigidbody.velocity = velocity; //set velocity changes
}
void Update ()
{
Vector3 inputV = playerCamera.transform.forward; //get camera relative controls
inputV.y = 0;
Vector3 inputH = playerCamera.transform.right;
inputH.y = 0;
Vector3 input = (inputV.normalized * Input.GetAxis("Vertical")) + (inputH.normalized * Input.GetAxis("Horizontal"));
inputV = transform.eulerAngles;
inputV.y = Mathf.Atan2(input.x, input.z) * Mathf.Rad2Deg;
transform.eulerAngles = inputV;
if (input.magnitude != 0) //if controls are pressed do acceleration calculations
{
Speed += Math.Abs(input.magnitude) * move.Acceleration;
if (Speed > move.MaxSpeed) Speed = move.MaxSpeed;
previousDirection = transform.eulerAngles;
}
else //else do deceleration calculations
{
Speed -= move.Deceleration;
if (Speed < 0) Speed = 0;
transform.eulerAngles = previousDirection;
}
Move (input * Speed); //execute Move (explained above)
if (Input.GetAxis("Action") != 0) //not in use currently, if action is pressed, print interacted object
{
RaycastHit rayCastInfo;
if (Physics.Raycast(transform.position + new Vector3(0,1,0), input, out rayCastInfo, 5))
print(rayCastInfo.collider.gameObject.name + " at " + rayCastInfo.point);
}
animation["walk"].speed = Speed / 4; //set walk animation speed
if (onGround) //if on the ground check and change animation (idle and walking)
{
if (Speed > 0 && animation.clip.name != "walk")
{
animation.CrossFade("walk");
}
else if (animation.clip.name != "idle")
{
animation.CrossFade("idle");
}
}
}
void OnCollisionStay(Collision collision)
{
onGround = false;
foreach(ContactPoint point in collision.contacts)
{
if (Vector3.Dot(new Vector3(0,1,0), point.normal) > 1 - slopeLimit) //if colliding with a surface within the ground limit, set onGround to true
{
onGround = true;
hasJumped = 0;
}
}
}
void OnCollisionExit(Collision collision) //on collision exit onGround = false, if player did not jump off surface, pretend they did.
{
onGround = false;
if (hasJumped == 0) hasJumped++;
}}
I'm unsure whats going on, nothing special runs when there is a multi-jump, its exactly the same as a normal jump except the players expected to already be airborne.
Any and all help is greatly appreciated, Thanks in advanced, Bombshell
Comment