i want to move my character in z axis with downKey keyboard button. but i cannot hold the button to make the player moving continuosly approaching the camera. if press and hold the button, player just forward one step. sorry if i make you confuss with my english… thx
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float grafity = -15f;
public float runSpeed = 8f;
//public var
//animation states
private int idleState = Animator.StringToHash ("Base Layer.idle");
private int runState = Animator.StringToHash ("Base Layer.walking");
private int Go_back = Animator.StringToHash ("Base Layer.Go_back");
private int Go_front = Animator.StringToHash ("Base Layer.Go_Front");
private int Go_left = Animator.StringToHash ("Base Layer.Go_left");
private CharacterController _controller;
private Animator _animator;
// Use this for initialization
void Awake()
{
_controller = GetComponent<CharacterController> ();
_animator = GetComponent<Animator> ();
}
void Update()
{
var velocity = _controller.velocity;
if (_controller.isGrounded) velocity.y = 0;
if (Input.GetKey (KeyCode.RightArrow))
{
velocity.x = runSpeed;
_animator.GoToStateIfNotAlreadyThere (runState);
}
else if (Input.GetKey (KeyCode.LeftArrow))
{
velocity.x = -runSpeed;
_animator.GoToStateIfNotAlreadyThere(Go_left);
}
else if (Input.GetKeyDown (KeyCode.UpArrow))
{
velocity.z = runSpeed;
_animator.GoToStateIfNotAlreadyThere (Go_back);
}
else if (Input.GetKeyDown (KeyCode.DownArrow))
{
velocity.z = -runSpeed;
_animator.GoToStateIfNotAlreadyThere (Go_front);
}
else
{
velocity.x = 0;
velocity.z = 0;
}
velocity.y += grafity * Time.deltaTime;
_controller.Move (velocity * Time.deltaTime);
}
private void GoRight()
{
if ( _controller.transform.localScale.x < 0f )
{
_controller.transform.localScale = new Vector3 (-_controller.transform.localScale.x, _controller.transform.localScale.y, _controller.transform.localScale.z);
}
}
private void GoLeft()
{
if ( _controller.transform.localScale.x > 0f )
{
_controller.transform.localScale = new Vector3 (-_controller.transform.localScale.x, _controller.transform.localScale.y, _controller.transform.localScale.z);
}
}
private void GoBack()
{
if ( _controller.transform.localScale.z > 0f )
{
_controller.transform.localScale = new Vector3 (-_controller.transform.localScale.x, _controller.transform.localScale.y, _controller.transform.localScale.z);
}
}
private void GoFront()
{
if ( _controller.transform.localScale.z > 0f )
{
_controller.transform.localScale = new Vector3 (-_controller.transform.localScale.x, _controller.transform.localScale.y, _controller.transform.localScale.z);
}
}
}