- Home /
Character not moving?
I am using the same script I used for "Project Stealth" I plan to make my own in the future with the same kind of idea but I am still new
Anyway for some reason the exact same script will make the project Stealth guy move but my bear won't move at all? The animations and howl work but it just doesn't move?
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public AudioClip HowlClip; // Audio clip for howling
public float turnSmoothing = 15f; // A smoothing value for turning
public float speedDampTime = 0.1f; // The damping for the speed parameter
private Animator anim; // Reference to the animator component.
private HashIDs hash; // Reference to the HashIDs.
// Use this for initialization
void Awake () {
// Setting up the references.
anim = GetComponent<Animator>();
hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
}
// Update is called once per frame
void FixedUpdate ()
{
// Cache the inputs.
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
bool hunt = Input.GetButton("Hunting");
MovementManagement(h, v, hunt);
}
void Update ()
{
// Cache the attention attracting input.
bool howl = Input.GetButtonDown("Howl");
// Set the animator shouting parameter.
anim.SetBool(hash.Howling, howl);
AudioManagement(howl);
}
void MovementManagement (float horizontal, float vertical, bool Hunting)
{
// Set the Hunting parameter to the hunting input.
anim.SetBool(hash.HuntingBool, Hunting);
// If there is some axis input...
if(horizontal != 0f || vertical != 0f)
{
Debug.Log("Move");
// ... set the players rotation and set the speed parameter to 5f.
Rotating(horizontal, vertical);
anim.SetFloat(hash.speedFloat, 5f, speedDampTime, Time.deltaTime);
}
else
// Otherwise set the speed parameter to 0.
anim.SetFloat(hash.speedFloat, 0);
}
void Rotating (float horizontal, float vertical)
{
// Create a new vector of the horizontal and vertical inputs.
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
// Create a rotation based on this new vector assuming that up is the global y axis.
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
// Create a rotation that is an increment closer to the target rotation from the player's rotation.
Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
// Change the players rotation to this new rotation.
rigidbody.MoveRotation(newRotation);
}
void AudioManagement (bool shout)
{
// If the player is currently in the run state...
if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.WalkLocoState)
{
// ... and if the footsteps are not playing...
if(!audio.isPlaying)
// ... play them.
audio.Play();
}
else
// Otherwise stop the footsteps.
audio.Stop();
// If the shout input has been pressed...
if(shout)
// ... play the shouting clip where we are.
AudioSource.PlayClipAtPoint(HowlClip, transform.position);
}
}
using UnityEngine;
using System.Collections;
public class HashIDs : MonoBehaviour
{
// Here we store the hash tags for various strings used in our animators.
public int WalkLocoState;
public int Howling;
public int HuntingBool;
public int speedFloat;
public int sneakingBool;
void Awake ()
{
WalkLocoState = Animator.StringToHash("Base Layer.Locomotion-Walk");
Howling = Animator.StringToHash("Howl");
speedFloat = Animator.StringToHash("Speed");
HuntingBool = Animator.StringToHash("Hunting");
}
}
I also get this error
'V05_PolarBear' AnimationEvent 'NewEvent' has no receiver! Are you missing a component?
Answer by BabilinApps · Dec 29, 2014 at 08:01 AM
Your character might not have room motion. A way to see if this is the case is to see if the character moves in the preview window. If he does not then you have to create your own room motion. To do this you can follow the documentation here: http://docs.unity3d.com/Manual/ScriptingRootMotion.html
sorry I should of been more specific, my bear is playing the animation, he is doing the animation on the spot and it is going into start walk and then looping walk while you hold "w" for example, but his transform position doesn't move