I have a game object that I can move around with a simple script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
private float moveSpeed;
private Vector3 playerPosition;
private Rigidbody rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
{
moveSpeed = 22;
} else
{
moveSpeed = 10;
}
player_movement();
}
private void player_movement()
{
if (Input.GetKey(KeyCode.W))
{
rb.MovePosition(Vector3.forward * moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
rb.MovePosition(Vector3.back * moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
rb.MovePosition(Vector3.left * moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
rb.MovePosition(Vector3.right * moveSpeed * Time.deltaTime);
}
}
}
However, the object keeps clashing with the flat piece of terrain I have set up and I don’t know why it is doing this. I have a small video of what it looks like when it happens here https://vid.me/dp9d. What is actually happening here?