playerMovement.cs
using System.Runtime;
using UnityEngine;
using UnityEngine.UI;
using static Unity.Burst.Intrinsics.X86.Avx;
using UnityEngine.AI;
public class playerMovement : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
public float moveSpeed = 20f;
Rigidbody rb;
public float rotationSpeed = 20f;
public Text outputText;
Vector3 dir = Vector3.zero;
Animator anim;
public Transform sitPoint;
public NavMeshAgent agent;
public Transform cookPoint;
bool up = false;
bool down = false;
bool left = false;
bool right = false;
bool sit = false;
bool sleep = false;
bool cook = false;
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
string cmd = outputText.text.ToLower();
up = cmd.Contains("up");
down = cmd.Contains("down");
left = cmd.Contains("left");
right = cmd.Contains("right");
sit = cmd.Contains("sit");
sleep = cmd.Contains("sleep");
cook = cmd.Contains("cook");
anim.SetBool("isWalking", up || down || left || right);
if(sit==false)
{
anim.SetBool("isSitting", sit);
}
anim.SetBool("isSleeping", sleep);
// Debug.Log("cook" + cook);
agent.enabled = true;
if (cook)
{
sit = false;
anim.SetBool("isSitting", false);
anim.SetBool("isWalking", true);
agent.SetDestination(cookPoint.position);
}
// Debug.Log("sit status" + sit);
if(sit)
{
anim.SetBool("isWalking", true);
agent.SetDestination(sitPoint.position);
}
if (agent.remainingDistance <= agent.stoppingDistance)
{
if (!agent.hasPath || agent.velocity.sqrMagnitude == 0f)
{
// ARRIVED
anim.SetBool("isWalking", false);
// Optional: Stop updating this so it doesn't repeat
cook = false;
sit = false;
}
}
}
void FixedUpdate()
{
dir = Vector3.zero;
if (up) dir += Vector3.forward;
if (down) dir += Vector3.back;
if (left) dir += Vector3.left;
if (right) dir += Vector3.right;
dir = dir.normalized;
if (dir != Vector3.zero)
{
rb.MovePosition(rb.position + dir * moveSpeed * Time.fixedDeltaTime);
Quaternion targetRot = Quaternion.LookRotation(dir);
rb.rotation = Quaternion.Slerp(rb.rotation, targetRot, rotationSpeed * Time.fixedDeltaTime);
}
}
}
sitOnChair
using UnityEngine;
using UnityEngine.AI;
public class sitOnChair : MonoBehaviour
{
public Transform sitPosition;
GameObject player;
Animator anim;
// Start is called once before the first execution of Update after the MonoBehaviour is created
public void Start()
{
player = GameObject.FindWithTag("Player");
anim = player.GetComponent<Animator>();
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
NavMeshAgent agent = other.GetComponent<NavMeshAgent>();
Rigidbody rb = other.GetComponent<Rigidbody>();
// Disable movement controllers
if (agent) agent.enabled = false;
//if (rb) rb.isKinematic = true; // Stop physics from interfering
player.transform.position = new Vector3(
sitPosition.position.x,
sitPosition.position.y+2f,
sitPosition.position.z + 5f
);
Debug.Log("trigger sit on the chair");
player.transform.rotation = Quaternion.Euler(0, 180, 0);
anim.SetBool("isSitting", true);
}
}
}