Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Player direction/idle animation 2 Answers

Animation not working when UI buttons are pressed 0 Answers

How do you change a players position during an animation? 2 Answers

Jump animation anticipation 2 Answers

How to prevent player from moving in air while jumping in place?? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges