5. Player interaction: interacting with the environment

Player rotation

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public Transform crosshair; // Reference to the crosshair object (aim target)

    void Update()
    {
        float horizontalInput = Input.GetAxisRaw("Horizontal");
        float verticalInput = Input.GetAxisRaw("Vertical");

        Vector3 moveDirection = new Vector3(horizontalInput, 0f, verticalInput).normalized;

        transform.Translate(moveDirection * moveSpeed * Time.deltaTime);


        // Rotate the player to face the crosshair
        Vector3 lookDirection = crosshair.position - transform.position;
        lookDirection.y = 0f; // Ensure the player doesn't tilt up or down
        if (lookDirection != Vector3.zero)
        {
            Quaternion targetRotation = Quaternion.LookRotation(lookDirection);
            transform.rotation = targetRotation;
        }
    }
}

using UnityEngine;

public class MinimapCameraMovement : MonoBehaviour
{
    public Transform player;
    public float followSpeed = 4f;
    public Vector3 offset = new Vector3(0, 100, 0);

    void LateUpdate()
    {
        transform.position = Vector3.Lerp(transform.position, player.position + offset, followSpeed * Time.deltaTime);
    }
}
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public Transform crosshair; // Reference to the crosshair object (aim target)
    public float rotateSpeed = 8f;

    private Vector3 moveDirection;

    void Update()
    {
        // Get input from the player
        float horizontalInput = Input.GetAxisRaw("Horizontal");
        float verticalInput = Input.GetAxisRaw("Vertical");

        // Calculate movement direction
        moveDirection = new Vector3(horizontalInput, 0f, verticalInput).normalized;
    }

    void FixedUpdate()
    {
        // Update player position
        transform.Translate(moveDirection * moveSpeed * Time.fixedDeltaTime, Space.World);

        // Rotate the player to face the crosshair
        Vector3 lookDirection = crosshair.position - transform.position;
        lookDirection.y = 0f; // Ensure the player doesn't tilt up or down
        if (lookDirection != Vector3.zero)
        {
            Quaternion targetRotation = Quaternion.LookRotation(lookDirection);
            transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.fixedDeltaTime * rotateSpeed);
        }
    }
}

Doors

Automatic sliding doors

using UnityEngine;

public class AutomaticDoorController : MonoBehaviour
{
    public Collider playerCollider; // Reference to the object that contains player's collider
    public GameObject door; // Reference to the sliding door object
    private Vector3 initialPosition; // Initial position of the door
    private Vector3 openPosition; // Target position when the door is open
    private Vector3 closedPosition; // Target position when the door is closed
    public float openSpeed = 2f; // Speed at which the door opens
    public float closeSpeed = 1f; // Speed at which the door closes

    private bool isPlayerInside = false; // Flag to track if the player is inside the trigger collider

    void Start()
    {
        initialPosition = door.transform.position;
        openPosition = initialPosition + Vector3.forward * 2.4f;
        closedPosition = initialPosition;
    }

    void Update()
    {
        if (isPlayerInside)
        {
            // Move the door gradually towards the open position
            door.transform.position = Vector3.Lerp(door.transform.position, openPosition, openSpeed * Time.deltaTime);
        }
        else
        {
            // Move the door gradually towards the closed position
            door.transform.position = Vector3.Lerp(door.transform.position, closedPosition, closeSpeed * Time.deltaTime);
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other == playerCollider)
        {
            isPlayerInside = true; // Player entered the trigger collider
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other == playerCollider)
        {
            isPlayerInside = false; // Player exited the trigger collider
        }
    }
}

Button-controlled doors

using UnityEngine;

public class ButtonController : MonoBehaviour
{
    public GameObject door;
    public GameObject player;

    void Update()
    {
        // Check if the player is pressing the "e" key
        if (Input.GetKeyDown(KeyCode.E))
        {
            // Check if the player is close to the button game object
            if (IsPlayerNearButton())
            {
                // Call a method on the door object to open it
                door.GetComponent<DoorController>().OpenDoor();
            }
        }
    }

    bool IsPlayerNearButton()
    {
        // YOUR CODE HERE
    }
}
using UnityEngine;
using System.Collections;

public class DoorController : MonoBehaviour
{
    // YOUR CODE HERE
    public float delayBeforeClose = 5f; // Delay before starting the door-closing process
    private bool doorOpening = false; // Flag to track if the door is opening

    void Start()
    {
        // YOUR CODE HERE
    }

    void Update()
    {
        // YOUR CODE HERE
    }

    public void OpenDoor()
    {
        Debug.Log("opening");
        doorOpening = true;

        // Start the coroutine for delaying the door closing
        StartCoroutine(DelayedClose());
    }

    IEnumerator DelayedClose()
    {
        // Wait for the specified delay before starting the door-closing process
        yield return new WaitForSeconds(delayBeforeClose);

        // Trigger the door-closing event
        CloseDoor();
    }

    public void CloseDoor()
    {
        Debug.Log("closing");
        doorOpening = false;
    }
}

Elevator


< ^ >