Object-following Virtual Buttons - c#

As shown in the picture, I want to be able to maintain this placement of virtual buttons wherever the ball moves.
This is what I've tried:
1.
The cue ball is a Rigidbody with Bounce Material, so it can bounce off the cushions. So in this case, I tried making the Virtual Buttons RigidBody Components and adding the same force to the buttons, but this didn't work because it wasn't able to detect the cushions and kept moving forward.
2.
I gave it a collider, it would bounce off the cushion before the ball and that would disrupt the formation around the ball.
3.
I tried to parent the Virtual Buttons under the Cue Ball and the Cue Ball under the Image Target, but this didn't help since it all centered within the ball and overlapped. I couldn't move it at all (I don't know why)
Willing to put a Bounty on the answer.

Virtual Butttons sometimes don't move above the Image Target, I really don't know why. But here's a work around.
Now consider your CueBall the parent. Throw in 8 Empty GameObjects as child Objects. Position these GameObjects in the position around the ball that you wish to maintain through the movement.
Now each virtual button is put as a child to these GameObjects respectively.
And now script movement of the ball. You should see it works perfectly. Make sure you have your X and Z rotation restriction. I guess since it's a Cue ball and its white, the rotation won't be much of a problem, unless you're scripting for Curve shots as well.
Hope this helps.

A solution might be to create a script to attach to your ImageTarget, that has 9 public transforms, (your 8 planes and the cue ball) that would then look at the cue ball's x and z coords, and then would change the planes' x and z coords accordingly.

Related

How to make an object not able to pass through an object? Unity

The title doesn't really explain much so ill expand more. I'm making a 2D game where you move a mouse to help you do stuff. I am using a fake mouse which is controlled by your mouse. And I want to make it so the mouse can't just move through walls. I've tried adding rigid bodies but the mouse still passes through it, or the wall just gets pushed by the mouse. (The mouse has a RigidBody2D script on it and a polygon collider 2D and the wall has a box collider 2D, and a RigidBody 2D) Thanks for the help.
If both have what you say, mouse has rigidbody + 2d-collider and walls have 2d-collider then it should work. A few things to note: it will only work if you move by physics (ie not teleporting each frame) and don't move it too fast (change rigidbody collision setting to "continous" if so). – Fredrik Schön
This solved it for me, Thanks

Unity C# - Collision sphere with cylinder - Not trigger sides of cylinder

I have a sphere collider on a stick and a cylinder. The cylinder has to detect a collision with the sphere collider of the stick and fire up a "OnTriggerEnter" script. Just imagine it like a drum stick hitting a drum in virtual reality.
Is there a way to avoid registering a collision on the sides of the cylinder, but only trigger on the top of the cylinder? And even without adding more protecting objects around it?
In the screenshot you see the properties of the stick and below the properties of the cylinder.
Obviously the green and red icons show which area is allowed to be triggered and which not.
Btw, simply using a thinner cylinder collider doesn't work in this case, because it wouldn't detect some collisions at all, when the stick goes too fast trough the thin cylinder. More about that in the comments.
Thank you.

Set limit camera screen WITDH

Look my game has a fixed camera, configured as a standard 1280x720.
I placed at each end of the cam width of an empty object with a collider to prevent the player pass this point.
Everything works perfectly until the default resolution decreases. When I run the test with 480x320 screen width decreases, but the collider, does not follow this decrease, staying out of the screen, which makes the player to be "cut off."
The following two images:
First: 1280x720
Second: 480x320
There is some way which I can only set the side margins left and right of the camera as a collider?
If it is not possible to configure only the edge of the camera as a delimiter, the player would have some other way to do something to solve my problem?
Some items fall from the sky, so I can not have a collider at the top of the camera.
There are many ways to do this. The easiest way is to create a 4 box collider 2D and position them to the edge of the screen.
I always use this complete script to do this. It's too long to post here. Just attach it to an empty GameObject in the scene. It will do the rest of the work for you. I will create the colliders and position them to the edge of the scrren automatically.
As what i have understand my be this can help you
Scale box collider to camera view

How do I make so when user moves the camera it doesn't go beyond the scene borders?

How do I make so when I move the camera(with touch) it doesn't go beyond the scene borders?
How do I move the camera with touch so it moves strictly with scene parts, like slides(swipe-first slide, another swipe-another slide) with not going beyond the borders of the scene?
The game I'm making has a camera like in Disco Zoo game for android (I'm a newbie)
Technically, the scene doesn't really have a border. You'll need to define the border somehow within your game, then constrain the camera's position with something like Mathf.Clamp(value, min, max) in an Update() function on the camera.
How can you define the border? It's up to you. Some ideas:
Hard-code the values in the script that clamps the camera. Probably the quickest option, but not flexible
Make public parameters on the camera script that let you set min and max positions in the X and Y directions
If you have a background image: use the extents of that to define your camera's extents
Create empty objects in your scene that define the minimum and maximum extents of the scene. Put your "min" object at the top-left, and the "max" object at the top-right. Connect it to the camera script, then use those positions to see if you've gone too far in any given direction. The main reason to do this is that it's visual.
(Slower, but dynamic) If everything in your scene uses physics, you could search the entire scene for every Collider component, then find the furthest extents in each direction. However, this is probably going to be pretty slow (so you'll only want to do it once), it'll take a while to code, and you'll probably want to tweak the boundaries by hand anyway.

making my player sprite land on top of my platform sprite

in my XNA game(im fairly new to XNA by the way) i would like to have my player sprite land on top of a platform. i have a player sprite class that inherits from my regular sprite class, and the regular sprite class for
basic non playable sprite stuff such as boxes, background stuff, and platforms. However, i am unsure how to implement a way to make my player sprite land on a platform.
My player Sprite can jump and move around, but i dont know where and how to check to see if it is on top of my platform sprite.
My player sprites jump method is here
private void Jump()
{
if (mCurrentState != State.Jumping)
{
mCurrentState = State.Jumping;
mStartingPosition = Position;
mDirection.Y = MOVE_UP;
mSpeed = new Vector2(jumpSpeed, jumpSpeed);
}
}
mStartingPosition is player sprites starting position of the jump, and Position is the player sprites current position. I would think that my code for checking to see whether my player sprite is on top of my platform sprite. I am unsure how to reference my platform sprite inside of the playersprite class and inside of the jump method.
i think it should be something like this
//platformSprite.CollisonBox would be the rectangle around the platform, but im not
//sure how to check to see if player.Position is touching any point
//on platformSprite.CollisionBox
if(player.Position == platformSprite.CollisionBox)
{
player.mDirection = 0;
}
Again im pretty new to programming and XNA, and some of this logic i dont quite understand so any help on any of it would be greatly appreciated:D
Thanks
If player.Position is a Point and CollisionBox is a Rectangle, you could use
if (platformSprite.CollisionBox.Contains (player.Position))
I was playing around with something like this just a few days ago.
What you're calling a CollisionBox I called a BoundingBox. A BoundingBox is a Rectangle which represents the area occupied by the sprite.
You'll probably find it helpful to define a BoundingBox for your sprites instead of just using their position.
You can easily test for the collision of Rectangles using the following code:
if (player.BoundingBox.Intersects(platform.BoundingBox)
{
// handle collision here...
}
For this to work, make sure that the X and Y coordinates of your BoundingBox are correctly reflecting your sprite's position.
You may want to look at the Platformer Starter Kit. It includes this as well as detecting when the player is touching an enemy, collecting gems, etc.
The answer about BoundingBox is right, but IMHO it is easier to use Rectangle if you are making a 2D game rather than BoundingBox, wich is designed for a 3D one.
Anyway both objects have intersection / collision test methods that takes Vector2 or Point as parameter.
Now you have to have a box for your player, and a box for your platform. If one collide with the other, you have to check from where (the player can hit the platform from up, down, left, right, or maybe up & left on the left up corner).
If the player is on top of the platform, then just stop his fall, probably by setting his Y-speed component to 0.
Maybe you will need the player to go through the platform when jumping (player hit from bottom) but not when falling (player hit from top).

Categories