- Home /
Bouncing object with new UI
Hello Unity community,
I am with one problem. If you look at the image attached you can see one small sprite at the center of the canvas. That gameobject starts with an force attached. The problem is:
When the gameobject (the simple sprite) hits the wall of the canvas, I want the sprite to bounce. The bouncing script isn't a problem for me: I can add a new force. The problem is detect that my sprite HITS the "wall". I don't want any "wall" on the screen.
Can you please tell me how I can detect that the sprite / gameobject hits the "wall"?
Thank you very much :)
Answer by KdRWaylander · Aug 16, 2015 at 11:02 PM
Hi,
Since you know the size in pixels of your sprite and of the canvas, check in the Update the position of your unicorn sprite: say your canvas is 1920x1080p and your sprite is 100x100p, if the position of your sprite (with anchors in the middle) is (-910,0), it will mean that it's touching on the left side of the canvas.
1920/2 = 960 so far left pixel has a x-coordinate of -960, sprite is 100px so anchor is at 50px in the middle, -960 + 50 = center of the sprite is at the exact position that gives the left border of the sprite on the left border of the canvas.
If it's not clear enough, let me know and i'll draw something :)
I don't understand why when I check the position of my sprite it give me like -300 (x-axis) on the left of the canvas and +300 at the right of the canvas. I use 1920x1080 on my Canvas as well..
Thank you for your help! :)
I need an image of your image's inspector. I think it's all about the way you anchored your sprite, try the red cross like in the middle (not a blue one that straches the sprite and computes its size based on how many pixels you want to leave on the left and on the right)
Okay $$anonymous$$dRWaylander, I already did it. I finally understood what you've said. Thank you :D
Your answer
Follow this Question
Related Questions
Using events to change variables and inherit that value? 0 Answers
Passing through a GameObject/Function to a button's OnClick 1 Answer
How to trigger OnValueChange(Vector 2) method from code? 1 Answer
Making a Menu like UDK in Unity 4.6 1 Answer
text and image prefab not popping up when set.active 2 Answers