Working on dual controls using character controller
I'm working on a character controller where the character moves forward regardless of input direction unless you are shooting then it goes to your typical shooter control scheme. I have the shooter control scheme working when the right mouse button or left trigger is pulled on a game pad except for camera controls. The camera works but will not stop rotating around the character when using the game pad. May need to adjust the dead value on the input manager.
So now onto when the player is not wanting to shoot so that they have free roam. If WASD or the left joystick is used, I want the character to move forward in the direction pressed. Here is my code, let me know what may work
public float speed = 6;
public float turnSpeed = 3;
public float gravity = 20.0f;
public float jumpSpeed = 6;
Animator animator;
//Transform cameraT;
CharacterController controller;
Vector3 moveDirection = Vector3.zero;
private void Start()
{
animator = GetComponent<Animator>();
//cameraT = Camera.main.transform;
controller = GetComponent<CharacterController>();
//let character fall
transform.position = new Vector3(0, 5, 0);
}
private void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
if (controller.isGrounded)
{
if (Input.GetButton("Fire2") || Input.GetAxis("Fire2") >0)
{
moveDirection = new Vector3(horizontal, 0.0f, vertical);
}
else
{
moveDirection = new Vector3(0, 0.0f, vertical);
}
moveDirection = transform.TransformDirection(moveDirection);
moveDirection = moveDirection * speed;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpSpeed;
animator.SetBool("jumpPressed", true);
}
else
animator.SetBool("jumpPressed", false);
}
moveDirection.y = moveDirection.y - (gravity * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
animator.SetFloat("speedPercent", vertical);
if (!controller.isGrounded)
animator.SetBool("grounded", false);
else
animator.SetBool("grounded", true);
}
Comment