I’m new to unity so bear with me. I’m working on a simple project, where I want the player to move using only the WASD keys and not the arrow keys (as I want to bind them to other actions).
I think I’m supposed to adjust things in the input manager but I’m not sure what to edit (assuming that’s where I’m supposed to look). I’ve tried to do some googling but I can’t seem to find anything that I can understand.
Any help would be appreciated.
Here is my playerMovement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
private Animator playerAnimator;
private float moveHorizontal;
private float moveVertical;
private Vector3 movement;
private Rigidbody playerRigidBody;
private bool isOnGround = true;
[SerializeField]
private float turningSpeed = 20f;
[SerializeField]
private float speed = 5f;
[SerializeField]
private float jumpHeight = 5f;
[SerializeField]
private float teleportDistance = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Gather input from the keyboard
moveHorizontal = Input.GetAxisRaw ("Horizontal" );
moveVertical = Input.GetAxisRaw ( "Vertical" );
if(Input.GetKeyDown(KeyCode.Space))
{
if (isOnGround == true) {
playerRigidBody.AddForce (0, jumpHeight, 0, ForceMode.Impulse);
}
}
else if(Input.GetKeyDown(KeyCode.UpArrow))
{
playerRigidBody.MovePosition(transform.position + transform.forward * teleportDistance);
//playerRigidBody.transform.position += playerRigidBody.transform.forward * 1f;
//playerRigidBody.AddForce (transform.forward * 2f, ForceMode.Impulse);
}
else if(Input.GetKeyDown(KeyCode.DownArrow))
{
playerRigidBody.MovePosition(transform.position - transform.forward * teleportDistance);
}
else if(Input.GetKeyDown(KeyCode.LeftArrow))
{
playerRigidBody.MovePosition(transform.position - transform.right * teleportDistance);
}
else if(Input.GetKeyDown(KeyCode.RightArrow))
{
playerRigidBody.MovePosition(transform.position + transform.right * teleportDistance);
}
else{
movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
Move (moveHorizontal, moveVertical, 0);
}
}
//handles game physics
void FixedUpdate () {
//Gather the animator component and rigidbody component from the player game object
playerAnimator = GetComponent<Animator> ();
playerRigidBody = GetComponent<Rigidbody> ();
//If the movement is not equal to zero
//then set speed and animate movement
//else stop moving and play idle animation
if( movement != Vector3.zero)
{
//create a target roatation based on the movement vector
Quaternion targetRotation = Quaternion.LookRotation(movement, Vector3.up);
//create rotation that moves from current rotation to target rotation
Quaternion newRotation = Quaternion.Lerp (playerRigidBody.rotation, targetRotation, turningSpeed * Time.deltaTime);
//change the players roatation to the new incremental rotation
playerRigidBody.MoveRotation(newRotation);
//playerAnimator.SetFloat ("Speed",3f);
}
else
{
//playerAnimator.SetFloat ("Speed", 0f);
}
}
void Move (float h, float v, float u)
{
if (u < 0)
{
u = 0.0f;
}
// Set the movement vector based on the axis input.
movement.Set (h, u, v);
// Normalise the movement vector and make it proportional to the speed per second.
movement = movement.normalized * speed * Time.deltaTime;
// Move the player to it's current position plus the movement.
playerRigidBody.MovePosition (transform.position + movement);
}
//make sure u replace "floor" with your gameobject name.on which player is standing
void OnCollisionEnter(Collision other){
if(other.gameObject.CompareTag ("Ground"))
{
isOnGround = true;
}
}
//consider when character is jumping .. it will exit collision.
void OnCollisionExit(Collision other){
if(other.gameObject.CompareTag ("Ground"))
{
isOnGround = false;
}
}
}