I have a rigidbody character with the animator component, capsule collider, rigidbody, and my movment script. My problem is is when I press the play button my player first doesn’t fall, and when I press forward, the player plays the running animation, but doesn’t move.
Here is my movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mover : MonoBehaviour
{
public GameObject Player;
public Rigidbody rb;
public GameObject Center;
public GameObject Facing;
public float JumpForce = 7.5f;
public float RunSpeed = 2f;
private bool canJump = true;
void jump()
{
if(canJump == true)
{
Player.GetComponent<Animator>().Play("Jumping");
rb.AddForce(0f, JumpForce, 0f, ForceMode.Impulse);
canJump = false;
//reset isJumping after 1.5 sec
Invoke("changeCanJump", 1.5f);
}
}
void changeCanJump()
{
canJump = true;
}
void OnCollisionEnter(Collision collisionInfo)
{
if(collisionInfo.collider.tag == "Enemy")
{
Player.GetComponent<Animator>().Play("KnockBack");
}
}
void Update()
{
if (Input.GetKey(KeyCode.D))
{
if (Facing.transform.position.z > Center.transform.position.z)
{
Player.transform.Rotate(0, 180, 0);
}
rb.AddForce(0, 0, -RunSpeed, ForceMode.Force);
Player.GetComponent<Animator>().Play("WalkCycle1");
} else if (Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.W))
{
//rb.AddForce(0, JumpForce * Time.deltaTime, 0, ForceMode.Impulse);
//Player.GetComponent<Animator>().Play("Jumping");
jump();
} else if (Input.GetKey(KeyCode.A))
{
if (Facing.transform.position.z < Center.transform.position.z) // -Z is forward, Z is backward
{
Player.transform.Rotate(0, 180, 0);
}
//Find the position of the facing gameobject and see what side it is relative to the middle point.
rb.AddForce(0, 0, RunSpeed, ForceMode.Force);
Player.GetComponent<Animator>().Play("WalkCycle1");
} else
{
Player.GetComponent<Animator>().Rebind ();
}
}
}