Hey guys i am torn between so many different answers out there im trying to get my player moving im not new to unity but this is my first third person RPG game it will have alot of mechanics from games like Fallout, borderlands and skyrim but as i said above it will be mainly third person and have waaaayyyyy better combat not exactly but similar to what batman and assassin creed does with their combat
thanks but as i said im NOT new to unity i know mecanim quite well its the only animation system i use but this has nothing to do with my question. Thanks anyways
The same what you are looking to do with rigidbody.adforce or charactercontroller.move can achieve with the mecanim system. That is why i suggested. Thanks
ahhhhh i see what your saying ok i thought you meant how to use mecanim my apologies i really am a little more curious about rigidbody option and or character controller tho but ill look into that also thanks Sabin
I’ve had problems using mecanim for handling movement, so I’d rather just use a movement script.
I’ll share what I usually use (a simplified version without my special methods), whether it’s first person or third.
I use InControl for my input though, and have input handled in a separate script. So this script may have a few small problems but nothing that the editor won’t tell you.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private bool _Jump;
private bool _Crouch;
private bool Running = false;
private float Speed = 0;
[HideInInspector]
public bool losingStamina = false;
[HideInInspector]
public bool canJump = true;
[HideInInspector]
public bool isMoving = true;
[HideInInspector]
public bool isGrounded = true;
public float walkSpeed = 10;
public float runSpeed = 15;
public float AirModifier = 1.5f;
public float fakeFriction = 0.86f;
public float jumpForce = 40;
public float JumpDisableTime = 0.2f;
public float isGroundCheck = 1;
void Start () {
_CameraRotBone = GetComponentInChildren<Camera> ().gameObject;
PlayerPrefs.SetFloat ("MovementDiv", 1);
}
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
//Speed Modifier
if (Running == true && isMoving && isGrounded) {
Speed = runSpeed;
} else if (Running && isMoving && isGrounded) {
Speed = walkSpeed;
} else{
Speed = walkSpeed/AirModifier;
}
if(h >= 0.1f || h <= -0.1f || v >= 0.1f || v <= -0.1f){
isMoving = true;
}else{
isMoving = false;
}
Move(new Vector3(h, 0, v), Input.GetButtonDown("Jump"), Input.GetButton("Run"), Input.GetButton("Crouch"), );
}
public void Move(Vector3 move, bool Jump, bool Run, bool crouch, bool Moving){
isGrounded = Physics.Raycast(transform.position, -Vector3.up, isGroundCheck);
Running = Run;
_Jump = Jump;
_Crouch = crouch;
//Limit Speed
if(rigidbody.velocity.magnitude > Speed){
rigidbody.velocity = rigidbody.velocity.normalized * Speed;
}
//Movement
this.rigidbody.velocity = new Vector3 (this.rigidbody.velocity.x * fakeFriction, this.rigidbody.velocity.y, this.rigidbody.velocity.z * fakeFriction);
Vector3 MovementForce = new Vector3 (move.x, 0, move.z);
MovementForce.Normalize ();
Vector3 MoveForce = transform.TransformDirection (MovementForce);
if (Moving) {
rigidbody.AddForce (MoveForce * Speed);
}
if (isGrounded && canJump && _Jump) {
rigidbody.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
}
IEnumerator JumpDisable(){
canJump = false;
yield return new WaitForSeconds(JumpDisableTime);
canJump = true;
}
}