Basically my 3rd person character ridiculously climbs up mountains
Is there anything I can add into this code to prevent my character from walking up the mountains?
I used mecanim for the movements and that forced me to use a rigid body which has no slope feature like the 1st person controller.
using UnityEngine;
using System.Collections;
// Require these components when using this script
[RequireComponent(typeof (Animator))]
[RequireComponent(typeof (CapsuleCollider))]
[RequireComponent(typeof (Rigidbody))]
public class LeonidasControlScript : MonoBehaviour
{
[System.NonSerialized]
public float lookWeight; // the amount to transition when using head look
[System.NonSerialized]
public Transform enemy; // a transform to Lerp the camera to during head look
public float animSpeed = 1.5f; // a public setting for overall animator animation speed
public float lookSmoother = 3f; // a smoothing setting for camera motion
public bool useCurves; // a setting for teaching purposes to show use of curves
private Animator anim; // a reference to the animator on the character
private AnimatorStateInfo currentBaseState; // a reference to the current state of the animator, used for base layer
private AnimatorStateInfo layer2CurrentState; // a reference to the current state of the animator, used for layer 2
private CapsuleCollider col; // a reference to the capsule collider of the character
static int idleState = Animator.StringToHash("Base Layer.Idle");
static int locoState = Animator.StringToHash("Base Layer.Locomotion"); // these integers are references to our animator's states
static int jumpState = Animator.StringToHash("Base Layer.Jump"); // and are used to check state for various actions to occur
static int spearState = Animator.StringToHash("Base Layer.Spear");
static int shieldState = Animator.StringToHash("Base Layer.Shield");
void Start ()
{
// initialising reference variables
anim = GetComponent<Animator>();
col = GetComponent<CapsuleCollider>();
if(anim.layerCount ==2)
anim.SetLayerWeight(1, 1);
}
void FixedUpdate ()
{
float h = Input.GetAxis("Horizontal"); // setup h variable as our horizontal input axis
float v = Input.GetAxis("Vertical"); // setup v variables as our vertical input axis
anim.SetFloat("Speed", v); // set our animator's float parameter 'Speed' equal to the vertical input axis
anim.SetFloat("Direction", h); // set our animator's float parameter 'Direction' equal to the horizontal input axis
anim.speed = animSpeed; // set the speed of our animator to the public variable 'animSpeed'
anim.SetLookAtWeight(lookWeight); // set the Look At Weight - amount to use look at IK vs using the head's animation
currentBaseState = anim.GetCurrentAnimatorStateInfo(0); // set our currentState variable to the current state of the Base Layer (0) of animation
if(anim.layerCount ==2)
layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1); // set our layer2CurrentState variable to the current state of the second Layer (1) of animation
// STANDARD JUMPING
// if we are currently in a state called Locomotion (see line 25), then allow Jump input (Space) to set the Jump bool parameter in the Animator to true
if (currentBaseState.nameHash == locoState )
{
if(Input.GetButtonDown("Jump"))
{
anim.SetBool("Jump", true);
}
}
// if we are in the jumping state...
else if(currentBaseState.nameHash == jumpState)
{
// ..and not still in transition..
if(!anim.IsInTransition(0))
{
if(useCurves)
// ..set the collider height to a float curve in the clip called ColliderHeight
col.height = anim.GetFloat("ColliderHeight");
// reset the Jump bool so we can jump again, and so that the state does not loop
anim.SetBool("Jump", false);
}
}
// Spear
// if we are currently in a state called Locomotion (see line 25), then allow Jump input (Space) to set the Jump bool parameter in the Animator to true
if (currentBaseState.nameHash == idleState )
{
if(Input.GetButtonDown("Spear"))
{
anim.SetBool("Spear", true);
}
}
// if we are in the spear state...
else if(currentBaseState.nameHash == spearState)
{
// reset the Spear bool so we can spear again, and so that the state does not loop
anim.SetBool("Spear", false);
}
// Shield
// if we are currently in a state called Idle , then allow Shield input (z) to set the Shield bool parameter in the Animator to true
if (currentBaseState.nameHash == idleState )
{
if(Input.GetButtonDown("Shield"))
{
anim.SetBool("Shield", true);
}
}
// if we are in the shield state...
else if(currentBaseState.nameHash == shieldState)
{
// reset the Shield bool so we can spear again, and so that the state does not loop
anim.SetBool("Shield", false);
}
}
}