- Home /
The question is answered, right answer was accepted
How to change the color of the ball after hitting square?
C# 2D
I have a multi colored square ( PIC I).
My BALL (PIC II) hits him.
How to change the color of the ball after hitting square?
The point is to make the ball change its color to the color it strikes.
E.g:
The ball hits the red part of square- ball turns red
The ball hits the green part of square - bal changes color to green
etc. etc.
I know I should use a collision, but I don't know how.
Answer by kskjadav007 · Apr 13, 2018 at 09:03 AM
i tried this and worked
void OnCollisionEnter2D(Collision2D coll)
{
if(coll.gameObject.tag=="Enemy")
{
Debug.Log ("Collision");
Color one= coll.gameObject.GetComponent <SpriteRenderer>().color;
gameObject.GetComponent <SpriteRenderer>().color=one;
}
}
here is demo
Hi.
Could you possibly show me screen of the ball and square in the Inspector?
I would be grateful if I saw how it looks there with the materials.
it was sprites-default mat just see the diffrence between my code and your
did you try my code ?
Ok, I give myself a last chance.
$$anonymous$$aybe now you'll tell me what I'm doing wrong.
Forgive for speed, but there is a limit to 1 $$anonymous$$ute :)
If you set a large screen, everything will be seen perfectly
Answer by tormentoarmagedoom · Apr 12, 2018 at 10:17 AM
Good day.
You should first learn how to manage collisions/triggers. I recommend you to create 1 empty object for each color, with a collider in each object, to know what color has been touched.
Then, you have to change the material of the ball (you must have a material for each color), using Renderer.material.
We can help with some script problem or specific doubt, but you must learn how to use it by your own.
Bye!
Ok, if you can explain one thing to me:
Can I use Renderer. $$anonymous$$aterial for 2D objects?
Because as a beginner I tried changing colors in 3D yesterday and I used it there, now I would like to try it in 2D, but unfortunately I do not know how to do it, that's why I asked a question.
I really have no idea :D I never did 2D things! lets see if someone knows.. :D
It killed me :)
But thanks for the comment, I appreciate the help :)
Yes, Renderer and $$anonymous$$aterial work the same way in 2D as well. In fact, Renderer is the base class for $$anonymous$$eshRenderer, LineRenderer, SpriteRenderer, and material
is a property of Renderer. However, you usually need to use different shaders for your materials for different rendering modes.
Just try the below code to see that it works:
Renderer r;
$$anonymous$$eshRenderer m = GetComponent<$$anonymous$$eshRenderer>();
r = m;
LineRenderer l= GetComponent<LineRenderer>();
r = l;
SpriteRenderer s= GetComponent<SpriteRenderer>();
r = s;
Answer by MarioSantoso · Apr 13, 2018 at 09:31 AM
Hi @polan31
All the answers above are pointing on the right direction. My suspect is that your colliders are not working properly.
Since you are playing in 2d, you must have a "Rigidbody 2D" on the circle object, and make sure it has "Circle Collider 2D" as well. As for the rectangles, make sure they have "Box Collider 2D"., the rectangles do not need to have rigidbody.
Answer by sean244 · Apr 12, 2018 at 08:57 PM
Add a "Target" tag to each colored shaped that the ball can hit. Then add the following code into your ball script
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Target")
{
GetComponent<SpriteRenderer>().color = other.gameObject.GetComponent<SpriteRenderer>().color;
}
}
I tried exactly the same code, only with a different tag ("ColorChanger").
Unfortunately, the code doesn't work in my case.
You should have 3 different gameobjects in your hierarchy: green, red and pink, each with the “ColorChanger” tag. Also, make sure that these 3 objects are NOT inside another object that has its own boxcollider.
I've been doing it for two or three days.
In a several combinations: with a smaller - larger box, with and without material etc. etc,
but it doesn't work.
But thanks for the effort you put in to help me
Answer by waikit97 · Apr 13, 2018 at 03:27 AM
Try creating three empty game objects, only add Box Collider and Sprite Renderer components for them. These three is not necessary to be seen by player.
1) adjust the scale of the box colliders.
The first collider is scaled and placed exactly as the size of the coloured square's green color region, the second collider matches the pink one and the third matches the red one.
2) change the sprite renderer's color as you wish.
Then, change the color in the sprite renderer of each collider object to the color of your coloured square respectively.
3) adding conditions.
Then use the onCollisionEnter method mentioned above by sean to change the ball's color. If it hits the first collider, it changes to green, if it hits the second it changes to pink and if it hits the red one it changes to red.
I've been doing it for two or three days.
In a several combinations: with a smaller - larger box, with and without material etc. etc,
but it doesn't work.
But thanks for the effort you put in to help me
I am sorry, but unfortunately I'm not at home now and I write on the phone from memory :)
But I hope I have not forgotten anything :) If you had any objections, write and I will be sure what and how, when I return home :)
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Target") {
Debug.Log ("Collision!");
{
GetComponent<SpriteRenderer> ().color = other.gameObject.GetComponent<SpriteRenderer> ().color;
}
}
}
Each hit was detected.
Follow this Question
Related Questions
Collision problem 0 Answers
2D box colliders not touching but are colliding, how to fix? 0 Answers
Weapon System with collide detection (Helps with script pls)!!! 0 Answers
Collision vs trigger 1 Answer
Modular Room System Similar to Mega Man 0 Answers