Right now I have a basic cube and a plane; the cube is the player and the plane is the platform. I’ve managed to create player movement on the plane, but I’m not sure how to restrict the cube to the plane so that the player doesn’t fall off. I’ve seen examples where people have used “if (transform.x.position> a number value)…”, but is there a way to restrict it to my plane specifically?
The only script I have is for the cube at the moment and my screen looks like this:
Here is my script for the cube so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cubeControls : MonoBehaviour
{
public float moveSpeed = 0.5F;
public Rigidbody player;
// Start is called before the first frame update
void Start()
{
// Setting the cube (player) color to red
Renderer rend = GetComponent<Renderer>();
rend.material.SetColor("_Color", Color.red);
// Needed to use rigid body methods
player = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// Moving the object right
if (Input.GetKey(KeyCode.RightArrow))
{
player.MovePosition(transform.position + transform.right * Time.deltaTime);
}
// Moving the object left
if (Input.GetKey(KeyCode.LeftArrow))
{
player.MovePosition(transform.position + transform.right * -Time.deltaTime);
}
// Moving the object forwards
if (Input.GetKey(KeyCode.DownArrow))
{
player.MovePosition(transform.position + transform.forward * -Time.deltaTime);
}
// Moving the object backwards
if (Input.GetKey(KeyCode.UpArrow))
{
player.MovePosition(transform.position + transform.forward * Time.deltaTime);
}
}
}