I make a cube object and attach main camera to the cube. Also I have another cube and cylinder in the area, so when I play, they should be invisible and after the cube or player has contact with object, it gets visible:
public class Player : MonoBehaviour
{
// Start is called before the first frame update
private float _speed = 1000f;
public GameObject yellow;
public GameObject red;
void Start()
{
yellow.gameObject.SetActive(false);
red.gameObject.SetActive(false);
}
// Update is called once per frame
void Update()
{
CalculateMovement();
}
void CalculateMovement()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
float x = 5f * Input.GetAxis("Mouse X");
float y = 5f * -Input.GetAxis("Mouse Y");
Vector3 direction = new Vector3(horizontalInput, 0, verticalInput);
transform.Translate(direction * _speed * Time.deltaTime);
transform.Rotate(y,x,0);
transform.rotation = Quaternion.Euler(transform.eulerAngles.x,transform.eulerAngles.y,0);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Collaborative_area"))
{
other.gameObject.SetActive(true);
}
if (other.gameObject.CompareTag("Dangerous_area"))
{
red.gameObject.SetActive(true);
}
}
}
And I get confused for which object I should define collider and rigidbody?