Hello, I have been using Unity’s First Person Controller to move my player. But, when I saw a video explaining why you shouldn’t (nothing bad about it, just that you should understand the code before using it) I started making my own. Here is the code:-
using UnityEngine;
using System.Collections;
public class TransformRotate : MonoBehaviour
{
public float moveSpeed = 5f;
public float turnSpeed = 50f;
void Update ()
{
if(Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.W))
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.S))
transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.A))
transform.Translate(-Vector3.right * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.D))
transform.Translate(-Vector3.left * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.W))
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.S))
transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.A))
transform.Translate(-Vector3.right * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.D))
transform.Translate(-Vector3.left * moveSpeed * Time.deltaTime);
}
}
Due to removing all of the First Person Controller Functions except for MouseLook.cs I found out that gravity was not there. When I added the Mesh Collider and RigidBody it falls through the floor (Cubes). It all works except for the Gravity. I want it to fall and “Use Gravity” but I don’t want it to fall through objects.
Bringing it back to my question, how do I make it so it won’t fall through the floor?
Any answers are appreciated.