Get collider and list it,Get Collider and List it
Hello everyone. What I want to do is; Putting 4 tire colliders in one area but counting down the tires and glowing green when you put the last tire in. I can turn the field green, but the colliders turn green as soon as they touch each other. I want the tires to glow green when they're all in. I would be very happy if anyone can help.
private Color m_oldColor = Color.white;
private void OnTriggerEnter(Collider other)
{
Renderer renderer = GetComponent<Renderer>();
m_oldColor = renderer.material.color;
renderer.material.color = Color.green;
}
private void OnTriggerExit(Collider other)
{
Renderer renderer = GetComponent<Renderer>();
renderer.material.color = m_oldColor;
}
the code i made to turn it green
but i cant get the colliders from the car controller script.
Answer by Senetry · Nov 04, 2021 at 07:41 PM
Hello have you considered adding tag to your tires or vehicle or adding tires on layer which do not collide with itself? There is this setting in project setting
You can create and set layer here
It is edit/project settings/physics 2d and Layer collision matrix you can set there what should collide and what not
Sorry I made reply for 2D but you can do same thing in 3D just choose Physics instead of Physics 2D
Answer by rsnrslt · Nov 05, 2021 at 10:17 PM
I'm sorry for asking stupid questions because I'm just getting started. . What I want to do is; All 4 tires glow green when they enter an area. I tried layers but no result. my tires have colliders and transforms. I need to use the count variable so that 4 tires enter the region I have determined. I don't know how to do this
@rsnrslt Can you post code you use for count? Also your new layer settings. And there are no stupid questions everybody starts somewhere.
Well you need to add static int variable which will increase when you hit collider of your basket.
Something like:
private Color m_oldColor = Color.white;
private static int count = 0;
private void OnTriggerEnter(Collider other)
{
count++;
}
private void OnTriggerStay(Collider other)
{
Renderer renderer = GetComponent<Renderer>();
if (count == 4)
{
m_oldColor = renderer.material.color;
renderer.material.color = Color.green;
}
else
{
renderer.material.color = m_oldColor;
}
}
private void OnTriggerExit(Collider other)
{
count--;
}
But to have layers set correctly is also important so it do no get called when it hits everything. Sorry I had to add lines to separate code because my browser do not format it correctly.
[SerializeField] private WheelCollider frontLeftWheelCollider; [SerializeField] private WheelCollider frontRightWheelCollider; [SerializeField] private WheelCollider rearLeftWheelCollider; [SerializeField] private WheelCollider rearRightWheelCollider;
these are colliders inside the carController script
I changed the layers and tags of my car's colliders. (tag = wheel1, layer = wheel)
(tag of parking side = square) , (layer = area)
What I want to do is to assign the colliders I wrote to the list. Then checking the list one by one to see if it goes inside the field. When I used the code you wrote and changed the layers, I still did not get a result.
the script is inside the parking area object(triggerScript)
List item
Answer by abadanoglu · Nov 07, 2021 at 02:50 PM
For those who are interested, the link of the package file
I made a mistake. I thought I added the file for the question I added myself. I am so sorry.
Your answer
Follow this Question
Related Questions
Physical Based Lighting/shadow Augmented Reality 1 Answer
Google Cardboard for Unity renders only one light source 1 Answer
Do I need to reconfigure materials for unity 2017? 1 Answer
Can I edit and add code after Unity3d project is ported to IOS? 0 Answers
How to hide gameobjects by layers? 0 Answers