hy guys i need help with this script, i have an instrument that can emet odor, in the game the player, with a vr, can approach the object that make odor and the idea behind is that i want an increment of the odor if the player mooved towards the object, but there is a problem with my instrument, the problem is that it cant make a costant increase but every 10 step the odor valve opened by 10, steps are calcolated thanks to the distance form the camera (vr) and the object. for my script i make 3 state for the valve of my instrument…the problem that i have is that che value of the valve and the steps dont increment. the script of the olfactometer is without error and is connected well with the instrument…sorry fod bad english, hope you can understand, oh and the values of maxdistance i see in the game the distance.
using UnityEngine;
public class Smellint : MonoBehaviour
{
public Transform playerCamera;
public Olfactometer olfactometer;
private float maxDistance = 1.9f;
private float maxValveOpening = 100f;
private float minValveOpening = 0f;
private float stepValveOpening = 10f;
private float valveOpening = 0f;
private int state = 0;
private const int STEP_COUNT = 100;
private float stepDistance;
private float lastStepDistance;
private int lastStep = 0;
private void Start()
{
stepDistance = maxDistance / STEP_COUNT;
lastStepDistance = 0f;
}
private int lastState = -1;
private float lastDistance = -1f;
private bool stateChanged = false;
private bool isValveOpened = false;
private void Update()
{
float distance = Vector3.Distance(transform.position, playerCamera.position);
if (lastState != state || Mathf.Abs(distance - lastDistance) >= 0.1f)
{
stateChanged = true;
}
if (stateChanged)
{
if (lastState != state)
{
//Debug.Log("Current state: " + state);
lastState = state;
}
if (Mathf.Abs(distance - lastDistance) >= 0.1f)
{
Debug.Log("Distance: " + distance);
Debug.Log("step: " + lastStep);
lastDistance = distance;
}
stateChanged = false;
}
if (distance > maxDistance)
{
state = 0;
// Debug.Log("state: " + state);
// Debug.Log("valveOpening: " + valveOpening);
}
else if (distance <= maxDistance)
{
state = 1;
// Debug.Log("state: " + state);
// Debug.Log("valveOpening: " + valveOpening);
}
if (state == 0)
{
valveOpening = 0f;
}
else if (state == 1 && !isValveOpened)
{
olfactometer.write("setValve 1\r");
olfactometer.write("readflow\r");
Debug.Log("Valve opened");
isValveOpened = true;
state = 2;
}
if (state == 2)
{
int step = Mathf.RoundToInt(distance / stepDistance);
Debug.Log("step: " + step);
if (step >= STEP_COUNT) step = STEP_COUNT - 1;
if (step > lastStep + 0.171 && valveOpening < maxValveOpening)
{
valveOpening = Mathf.Clamp(valveOpening + stepValveOpening, minValveOpening, maxValveOpening);
olfactometer.write("steps " + valveOpening + "\r");
Debug.Log("valve opening set to: " + valveOpening);
lastStep = step;
}
else if (step > lastStep - 0.171 && valveOpening > minValveOpening)
{
valveOpening = Mathf.Clamp(valveOpening - stepValveOpening, minValveOpening, maxValveOpening);
olfactometer.write("steps " + valveOpening + "\r");
Debug.Log("valve opening set to: " + valveOpening);
lastStep = step;
}
lastStepDistance = distance;
}
}
}