Hey, so I am new to unity, and am having problems with my player going through walls when I tell the player to move. The game is just basic movement inside of a room right now.
Here is the code that I am using for the playercontroller:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float speed = 0.1f;
//holds which key is currently being pressed (none if none)
private KeyCode keyPressed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//move right
if(Input.GetKey(KeyCode.RightArrow) && keyPressed == KeyCode.None
|| Input.GetKey(KeyCode.RightArrow) || keyPressed == KeyCode.RightArrow)
{
transform.position += transform.right * speed;
keyPressed = KeyCode.RightArrow;
}
//move left
if(Input.GetKey(KeyCode.LeftArrow) && keyPressed == KeyCode.None
|| Input.GetKey(KeyCode.LeftArrow) && keyPressed == KeyCode.LeftArrow)
{
transform.position += -transform.right * speed;
keyPressed = KeyCode.LeftArrow;
}
//move forward
if(Input.GetKey (KeyCode.UpArrow) && keyPressed == KeyCode.None
|| Input.GetKey(KeyCode.UpArrow) && keyPressed == KeyCode.UpArrow)
{
transform.position += transform.forward * speed;
keyPressed = KeyCode.UpArrow;
}
//move backward
if(Input.GetKey (KeyCode.DownArrow) && keyPressed == KeyCode.None
|| Input.GetKey(KeyCode.DownArrow) && keyPressed == KeyCode.DownArrow)
{
transform.position += -transform.forward * speed;
keyPressed = KeyCode.DownArrow;
}
if(Input.GetKeyUp(keyPressed)) {
keyPressed = KeyCode.None;
}
}
}