Reputation: 1
i have a problem with my code. i need to make a project for college - sims in unity. the player ai, needs and etc. i want to increase each need to 100 when the player is close to the target, e.g. the player has 0 hunger points. he goes to the kitchen. the eating animation starts and the hunger value goes up. when it reaches 100 points, the player goes to the next need, which also has 0 points.
but the problem is that when it reaches 0, it never goes up. something like it goes up and down at the same time. (im new in c# so sorry if my code is chaotic)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PlayerMove : MonoBehaviour {
public GameObject targetFood;
public GameObject targetSleep;
public GameObject targetDance;
public GameObject targetRead;
private NavMeshAgent agent;
private Animator animator;
[Header("Needs")]
public float hunger = 10f;
public float sleepiness = 50f;
public float dance = 80f;
public float reading = 100f;
[Header("Parameters")]
public float hungerRate = 4f;
public float sleepinessRate = 4f;
public float danceRate = 4f;
public float readingRate = 4f;
void Start()
{
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
}
void Update()
{
bool isIncreasing = false;
if (hunger <= 0 && Vector3.Distance(agent.transform.position, targetFood.transform.position) <= 1f)
{
hunger += hungerRate * 10 * Time.deltaTime;
if (hunger > 100)
{
hunger = 100;
isIncreasing = false;
}
else
{
isIncreasing = true;
}
}
else if (hunger >= 100)
{
hunger -= hungerRate * Time.deltaTime;
if (hunger < 0)
{
hunger = 0;
isIncreasing = true;
}
else
{
isIncreasing = false;
}
}
else
{
if (isIncreasing)
{
hunger += hungerRate * 10 * Time.deltaTime;
if (hunger > 100)
{
hunger = 100;
isIncreasing = false;
}
}
else
{
hunger -= hungerRate * Time.deltaTime;
if (hunger < 0)
{
hunger = 0;
isIncreasing = true;
}
}
}
CheckNeeds();
}
void CheckNeeds()
{
if (hunger <= 0)
{
animator.SetBool("isEating", true);
MoveToTarget(targetFood.transform.position);
}
else
{
animator.SetBool("isEating", false);
}
if (sleepiness <= 0)
{
animator.SetBool("isSleeping", true);
MoveToTarget(targetSleep.transform.position);
}
else
{
animator.SetBool("isSleeping", false);
}
if (dance <= 0)
{
animator.SetBool("isDancing", true);
MoveToTarget(targetDance.transform.position);
}
else
{
animator.SetBool("isDancing", false);
}
if (reading <= 0)
{
animator.SetBool("isReading", true);
MoveToTarget(targetRead.transform.position);
}
else
{
animator.SetBool("isReading", false);
}
}
void MoveToTarget(Vector3 targetPosition)
{
agent.SetDestination(targetPosition);
}
}
so I decided to move the "isIncreasing" declaration to where the other variables are, and it started working. only now there is a problem that the value immediately goes to zero from 100 even if the player has not reached a certain place.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PlayerMove : MonoBehaviour
{
public GameObject targetFood;
public GameObject targetSleep;
public GameObject targetDance;
public GameObject targetRead;
private NavMeshAgent agent;
private Animator animator;
private bool isIncreasing;
[Header("Needs")]
public float hunger = 10f;
public float sleepiness = 50f;
public float dance = 80f;
public float reading = 100f;
[Header("Parameters")]
public float hungerRate = 4f;
public float sleepinessRate = 4f;
public float danceRate = 4f;
public float readingRate = 4f;
void Start()
{
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
}
void Update()
{
if (hunger <= 0 && Vector3.Distance(agent.transform.position, targetFood.transform.position) <= 1f)
{
hunger += hungerRate * 10 * Time.deltaTime;
if (hunger > 100)
{
hunger = 100;
isIncreasing = false;
}
else
{
isIncreasing = true;
}
}
else if (hunger >= 100 )
{
hunger -= hungerRate * Time.deltaTime;
if (hunger < 0)
{
hunger = 0;
isIncreasing = true;
}
else
{
isIncreasing = false;
}
}
else
{
if (isIncreasing)
{
hunger += hungerRate * 10 * Time.deltaTime;
if (hunger > 100)
{
hunger = 100;
isIncreasing = false;
}
}
else
{
hunger -= hungerRate * Time.deltaTime;
if (hunger < 0)
{
hunger = 0;
isIncreasing = true;
}
}
}
CheckNeeds();
}
void CheckNeeds()
{
if (hunger <= 0)
{
animator.SetBool("isEating", true);
MoveToTarget(targetFood.transform.position);
}
else
{
animator.SetBool("isEating", false);
}
if (sleepiness <= 0)
{
animator.SetBool("isSleeping", true);
MoveToTarget(targetSleep.transform.position);
}
else
{
animator.SetBool("isSleeping", false);
}
if (dance <= 0)
{
animator.SetBool("isDancing", true);
MoveToTarget(targetDance.transform.position);
}
else
{
animator.SetBool("isDancing", false);
}
if (reading <= 0)
{
animator.SetBool("isReading", true);
MoveToTarget(targetRead.transform.position);
}
else
{
animator.SetBool("isReading", false);
}
}
void MoveToTarget(Vector3 targetPosition)
{
agent.SetDestination(targetPosition);
}
}
Upvotes: 0
Views: 62
Reputation: 13
While I'm not completely sure what effect you are going for, this rewrite of your Update()
method might help:
void Update()
{
if (Vector3.Distance(agent.transform.position, targetFood.transform.position) <= 1f)
{
if (hunger < 100)
{
animator.SetBool("isEating", true);
hunger += hungerRate * 10 * Time.deltaTime;
if (hunger > 100)
{
animator.SetBool("isEating", false);
hunger = 100;
}
}
}
else
{
animator.SetBool("isEating", false);
if (hunger > 0)
{
hunger -= hungerRate * Time.deltaTime;
if (hunger < 0)
{
hunger = 0;
}
}
}
CheckNeeds();
}
void CheckNeeds()
{
if (hunger <= 0)
{
MoveToTarget(targetFood.transform.position);
}
// . . . the rest
}
This code would prevent values from going up and down at the same time, although the hunger would go down as soon as you move away from the target, and I'm not sure if that's what you're going for. If you're trying to make it decrease slower, then you can change the hunger rate. If you want it to start decreasing at a later time, then you can add a condition to the if (hunger > 0)
line in the above code for when you want it to start decreasing. This way of coding it also makes the isIncreasing
field not necessary.
Upvotes: 0