I’m very new with Unity and especially with C#. I’m creating a VR environment to use with my HTC Vive on a treadmill (I know it’s a wierd idea, don’t judge me).
Attached, you have a picture. On this picture, you can see my camera with a collider, another collider in front of it, another collider behind the camera and a green rectangle.
Background:
The camera is attached to an object. This object and the rectangle will both translate from a point A to a point B at the same speed (no problem with that, it works). So even if you are not moving in real life, the camera is moving forward on a straight line in the game with a selected speed and the green rectangle will stay in front of me. While the camera is automatically moving forward, I can still move in space with my headset (a little behind, a little forward,… I don’t know if it’s understandable). So the camera is moving foward but I can still walk in any direction around the moving object attached to my camera. This method gives me an optic flow while I’m walking on the treadmill. But of course I don’t see if I’m walking in the middle of the treadmill in real life because I have the headset in front of my eyes so I imagined the following technique.
To be quick, I want the green rectangle to turn blue if the collider of my camera touches the collider in the front and I want the rectangle to turn red if the collider of my camera touches the collider in the back. And if the collider of my camera doesn’t touch one of the two other colliders, I want the rectangle to turn green again.
The point is to know if I’m getting too close or too far to the rectangle. If I’m too close it turns blue and if I’m too far it turns red. The point of course is to to know if I’m walking at the right speed on my treadmill: if the rectangle is green, I’m ok, if it turns blue I’ve got to reduce my gait speed because I’m too quick and if it turns red I’ve got to accelerate on the treadmill because I risk to fall and to end in Fail Army if by chance someone is filming.
I tried some codes but it didn’t work… Someone can help me ? I don’t think it’s difficult but C# is still complicated for me and I always use turorials on youtube or forums like here… But this time, I d’ont find the solution.
Sounds like a reasonable idea, and shouldn’t be too hard.
I wouldn’t overcomplicate it… just make a script with a couple of public properties, float minZ and float maxZ. Set these to the forward and backward limits.
Then in Update, compare your position to these limits. For starters, just print out (with Debug.Log) when you exceed one of them. Then you can work on changing the color of the rectangle.
And post your actual code here when you get stuck. It’s hard to help you when we can’t see what you’re trying.
Thanks for you answer ! Here you will find some code to understand what I want to do.
First I simply managed to activate Debug.Log messages with the collider in front of me. I put a collider and a rigid body on my right Vive controller, I gave it the “Player” tag and when it collides with the collider in front of me, “Too fast” appears in the console. When it stops collide, “Ok” appears in the console. Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColorTooFast : MonoBehaviour
{
public void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
Debug.Log("Too Fast");
}
}
public void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
Debug.Log("Ok");
}
}
}
Simple and it works well. I put “public void” so that I can communicate with an other script on my rectangle. Here is the script of my green rectangle to change the material of it when I’m too fast on the treadmill :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RectangleColor : MonoBehaviour {
public Material[] material;
public ChangeColorTooFast otherScript;
Renderer rend;
void Update ()
{
//If the following condition is met in the other script, the rectangle turns blue
if (otherScript.GetComponent<ChangeColorTooFast>().OnTriggerEnter(Collison other))
{
rend.sharedMaterial = material[1];
}
//If the condition isn't met, so if my controller doesn't touch the collider in the front, the rectangle turns green again
else
{
rend.sharedMaterial = material[0];
}
}
}
Material 0 is green and material 1 is blue. But unfortunately it didn’t work. The problem seems to come from the if statement where OnTriggerEnter(Collision other) is not good.
if (otherScript.GetComponent().OnTriggerEnter(Collison other))
Simply put, this is not how it works. An if statements tests if something is True, and the OnTriggerEnter is not something that can be true because it returns nothing (that is what the void means!)
When Unity detects a collision it calls the OnTriggerEnter method. When you want something to happen there you have to put it in that method. What if you merge the two scripts and set the material in the OnTriggerEnter.
Thanks , i solved my problem !
It was very simple in fact. Here is the final code that I put on the collider in front of my camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColorTooFast : MonoBehaviour
{
public Material[] material;
public GameObject Chaperon;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
Chaperon.GetComponent<Renderer>().sharedMaterial = material[1];
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
Chaperon.GetComponent<Renderer>().sharedMaterial = material[0];
}
}
}
The GameObject called “Chaperon” is my rectangle. Material 0 is green and material 1 is blue. It was very simple ! I did the same for the collider behind the camera but the material 1 is red this time. So now if I walk too fast, the collider on my camera touches the collider in front and then the rectangle turns green. I slow down my pace, it turns green again. And it’s the same with the other collider if I walk too slow, the rectangle turns red. I walk faster and then the rectangle turns green again.
Maybe it will be useful for other poeple who want to walk in VR on their treadmill