- Home /
Script only makes character run forward
Hi,
Trying to use the Locomotion starter pack script from the Asset store to try and make my own fully functioning controller. I have one problem though, the script seems to always be making my character move forwards i.e. going from 0 to 6 speed float (Mecanim) when I press S as well as W. I can't really figure out what part I am missing.
public class JoystickToEvents : MonoBehaviour
{
public static void Do(Transform root, Transform camera, ref float speed, ref float direction)
{
Vector3 rootDirection = root.forward;
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 stickDirection = new Vector3(horizontal, 0, vertical);
// Get camera rotation.
Vector3 CameraDirection = camera.forward;
CameraDirection.y = 0.0f; // kill Y
Quaternion referentialShift = Quaternion.FromToRotation(Vector3.forward, CameraDirection);
// Convert joystick input in Worldspace coordinates
Vector3 moveDirection = referentialShift * stickDirection;
Vector2 speedVec = new Vector2(horizontal, vertical);
speed = Mathf.Clamp(speedVec.magnitude, -1, 1);
if (speed > 0.01f) // dead zone
{
Vector3 axis = Vector3.Cross(rootDirection, moveDirection);
direction = Vector3.Angle(rootDirection, moveDirection) / 180.0f * (axis.y < 0 ? -1 : 1);
}
else
{
direction = 0.0f;
}
}
}
Is there a line of code im missing in order to get my WalkBack state to start working correctly? Thanks!
John
Answer by bodec · Jun 27, 2013 at 09:27 PM
speed in there should be between -1 and 1 0 meaning standing the animations will control your speed that is set in the animation setup. coding for mecanim is a bit different than a character controller instead of coding speed being how fast to move you will code 1 being moving forward at full speed and -1 moving backward at full speed.
here is a simple movement script to help understand it
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
private Animator _droidAnimator;
private float _horMove;
private float _vertMove;
public float animSpeed = 1.5f;
// Use this for initialization
void Start () {
_droidAnimator = GetComponent<Animator>();
}
void LateUpdate () {
_horMove = Input.GetAxis("Horizontal");
_vertMove = Input.GetAxis("Vertical");
_droidAnimator.SetFloat("Direction", _horMove);
_droidAnimator.SetFloat("Speed", _vertMove);
_droidAnimator.speed = animSpeed;
// if(_controller.isGrounded){
if(Input.GetButtonDown("Jump")){
_droidAnimator.SetBool("Jump", true);
}else{
_droidAnimator.SetBool("Jump", false);
}
}
}
and a screen shot of my tree
Tried your script and quickly changed $$anonymous$$e over to your more understandable one. this was a very nice starting point and fixed my problems with walking backwards and I got the jumping referenced in the script to work easily.
Thankyou $$anonymous$$r. Bodec! :)
Your answer
Follow this Question
Related Questions
How to access Blend Tree "speed" in Mecanim from C# 0 Answers
Left Click Script Problems 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Is there anyway to only show a part of a GUI image during the game? (C#) 1 Answer