- Home /
Input Manager
Hey guys I'm been trying to move a character using the input manager. I was able to have the character move forward, but he doesn't move backwards though. I have my S key as the negative button and I was sure it would turn my positive value into a negative value. Here is my code on how i'm moving my character
using UnityEngine;
using System.Collections;
// Require these components when using this script
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(Rigidbody))]
public class Wolf_AnimationInput : MonoBehaviour {
public float animSpeed = 1.5f; // a public setting for overall animator animation speed
public float lookSmoother = 3f; // a smoothing setting for camera motion
public float moveSpeed = 0.5f;
public float rotateSpeed = 180;
private Animator anim;
private AnimatorStateInfo currentBaseState;// a reference to the animator on the character
private CapsuleCollider col; // a reference to the capsule collider of the character
private CharacterController controller;
private Transform _mytransform;
static int idleState = Animator.StringToHash("Base Layer.Idle");
static int runState = Animator.StringToHash("Base Layer Run");
static int attackState = Animator.StringToHash("Base Layer.Attacks");
// Use this for initialization
void Start () {
_mytransform = transform;
anim = GetComponent<Animator>();
col = GetComponent<CapsuleCollider>();
}
// Update is called once per frame
void Update () {
float v = Input.GetAxis("Vertical"); // setup v variables as our vertical input axis
anim.SetFloat("Speed", v);
anim.speed = animSpeed;
currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
if (currentBaseState.nameHash == idleState)
{
if (Input.GetButtonDown("Attack"))
{
anim.SetBool("Atk", true);
}
else
{
anim.SetBool("Atk", false);
}
}
if(Input.GetButton("Vertical"))
{
_mytransform.Translate(new Vector3(0.0f, 0.0f, 0.0f) * Time.deltaTime);
}
if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0)
{
_mytransform.Rotate(0, Input.GetAxis("Horizontal") * Time.deltaTime * rotateSpeed, 0);
}
}
}
if(Input.GetButton("Vertical")) { _mytransform.Translate(new Vector3(0.0f, 0.0f, 0.0f) * Time.deltaTime); }
What are you trying to do with these lines?
Please delete or answer the question (and accept the answer using the checkmark) if you don't need any help.
Answer by Juan105ify · Jul 21, 2014 at 07:55 PM
Hey guys i Fixed the problem. The problem was i was setting the Value of the Vertical to 2. So it would always be a positive number. So i looked up and saw that using the GetAxis would fix the problem
Your answer
