- Home /
Add keyboard input in ClickToMove
How to add keyboard handling in this script Click To Move.
I would like to move using the AWSD keys also.
The player only needs to move going forward, move to the left, it turns the body into left and goes ahead normally.
using UnityEngine;
using System.Collections;
public class ClickToMove2 : MonoBehaviour {
public float speed;
public CharacterController controller;
private Vector3 position;
void Start () {
position = transform.position;
}
void Update ()
{
if (Input.GetMouseButton (0)) {
locatePosition();
}
moveToPosition ();
}
void locatePosition()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, 150)) {
position = new Vector3 (hit.point.x, hit.point.y, hit.point.z);
//Debug.Log (position);
}
}
void moveToPosition()
{
if (Vector3.Distance (transform.position, position) > 1)
{
Quaternion newRotation = Quaternion.LookRotation (position - transform.position);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * 10);
controller.SimpleMove(transform.forward * speed);
animation.CrossFade("run");
}
else
{
animation.CrossFade("idle");
}
}
}
Comment
Your answer
Follow this Question
Related Questions
input.GetAxis (float) not working, but Raw works. 1 Answer
Problems with the movement of the player 1 Answer
How do I listen for Input from joystick/buttons on a controller? 1 Answer
Two near-identical scripts cause a cat to jump in different ways - why? 2 Answers
Vector 3 - move on only one axis 0 Answers