- Home /
How to detect different parts in sprite?
I am making car game and I have a track but I don't know how to detect when the car is driving on sand. There is the picture below. When the car is driving on track it accelerates and when the car is driving on sand it is slowing speed. Track and sand is one png sprite and it is placed on the cube.
Answer by Bilelmnasser · Sep 25, 2017 at 07:53 AM
Hi, you can use PolygonCollider2D and check trigger checkbox and than use Functions like : OnTriggerEnter(Collider) And OnTriggerExit(Collider) to detect your car is in witch type of sprite .
this is the simple way to do it.
alt text This is my settings but it is not working. Black area is transparent in the image. I can add sand layer different time.
Yes, do some research on colliders. Here is a short Unity tutorial I found that explains how this works: https://unity3d.com/learn/tutorials/topics/physics/colliders-triggers
I think that polygon collider is not set to my track
Also, this is not normal collider. I have to detect different part of the sprite which is kind of difficult.
You can detect Textule color at a Raycast hit point, but you will need to consider that it quickly brakes down once you don't have flat colors you can go by.
$$anonymous$$ight worth checking the performance of that approach as well. It's probably more intended to be used for something like when the user uses a color picker every once in a while. Not sure what the performance hit is when you do it every FixedUpdate.
I'm guessing, someone with experience in making car games with this effect can give you some good ideas / tricks to achieve this.
Answer by hexagonius · Sep 25, 2017 at 08:37 AM
Since your colors are very clear, you can make use of this to get the actual color you're driving over:
https://docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html
Can you write me an example? I don't understand how to use this.
Your answer
Follow this Question
Related Questions
Does OnTriggerStay don't detect a collision with a non-trigger collider? 1 Answer
Collision between two instances of the same object 1 Answer
How do I use colliders and/or triggers to end the game? 1 Answer
Colliding with multiple objects 1 Answer
Change NavMesh Speed after collision/trigger just for 1 object 1 Answer