Seriously i cant find anything anywhere to help with this so I finally made an account to ask. As far as I can tell all the code should work. And the animation for Jumping definitely works. I tried changing it with the Run and it worked fine. but for some reason in the jump function it refuses to actually animate in testing. please help.
Updated code:
using UnityEngine;
using System.Collections;
public class Movementology : MonoBehaviour {
public float runSpeed = 10;
public float runAnimSpeed = 1;
public float walkSpeed = 4;
public float rotateSpeed = 120;
public float jumpSpeed = 300;
public float jumpAnimSpeed = 1;
//private bool isGrounded = false;
private bool jumping = false;
// Use this for initialization
void Start () {
animation["Jump"].layer = 4;
}
// Update is called once per frame
void Update () {
float moveForward = runSpeed * Time.smoothDeltaTime * Input.GetAxis("Vertical");
float moveBackward = walkSpeed * Time.smoothDeltaTime * Input.GetAxis ("Vertical");
//float moveLeft = moveSpeed * Time.smoothDeltaTime * Input.GetAxis("Horizontal");
float rotate = rotateSpeed * Time.smoothDeltaTime * Input.GetAxis("Horizontal");
if(!jumping && Input.GetKey(KeyCode.Space))
{
jumping = true;
rigidbody.AddForce(Vector3.up * jumpSpeed);
animation.CrossFade("Jumping");
}
else if(Input.GetAxis("Vertical") > 0)
{
transform.Translate(Vector3.forward * moveForward);
animation.CrossFade("Run");
}
else if(Input.GetAxis("Vertical") < 0)
{
transform.Translate(Vector3.forward * moveBackward);
//animation.CrossFade("Run");
}
else
{
animation.CrossFade("Idle");
}
//transform.Translate(Vector3.left * moveLeft);
transform.Rotate(Vector3.up, rotate);
}
void OnCollisionEnter(Collision collision){
if(collision.contacts.Length > 0)
{
ContactPoint contact = collision.contacts[0];
if(Vector3.Dot(contact.normal, Vector3.up) > 0.5)
{
jumping = false;
}
}
}
}