private void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
With this code I can touch the point on the screen but when I catch my balls on the screen sometimes i can't because they are too fast.
So can i take the point of click and enlarge this point ?
You can use a 2D or 3D Sphere collider and attach it to Empty gameobject , move that game object towards touch points on screen and use OnCollisionEnter2D() or OnCollisionEnter3D() event to detect detections with falling balls. Now the sphere collider is as a whole a point and its radius can altered in inspector to enlarge the points
More about On Collision Event : https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
Here's what worked for me:
Before (Inspector View):
BallGameObject
- Rigidbody
- Script that listens to OnMouseDown
After (Inspector View):
BallGameObject
- Rigidbody
- TouchDetectGameObject
- Circle Collider 2d (**Use a Sphere collider if you're in 3d!)
- Radius: 5
- Script that listens to OnMouseDown
Basically, add a child object with no material so it's invisible and its own Circle Collider + OnMouse listeners. Make the child object bigger than the parent. With no other changes, I can now click on my Ball even if I'm off to the side a bit with my touch.
Related
I am working on a 2D game with a Grid with 2 tilemaps in it. The walkable tilemap and the obstacles tilemap. I gave the obstacle tilemap the tilemapcollider2d. I want my player to have a kinematic rigidbody so physics won't do weird things after colliding with the obstacle tiles.
The thing is, the player only collides with the obstacle tiles if it has a dynamic rigidbody. How can I make the player collide with the obstacle tiles, while having a kinematic rigidbody?
I also tried adding a rigidbody2d to the obstacles tilemap but this doesn't have any effect. Unless it's set to dynamic, then all the obstacle tiles start falling down but do collide with the player for a moment before the player clips through it.
This the code for the movement of my player (body = the RigidBody2D of the player):
void Update()
{
// Gives a value between -1 and 1
horizontal = Input.GetAxisRaw("Horizontal"); // -1 is left
vertical = Input.GetAxisRaw("Vertical"); // -1 is down
}
void FixedUpdate()
{
if (horizontal != 0 && vertical != 0) // Check for diagonal movement
{
// limit movement speed diagonally, so you move at 70% speed
horizontal *= moveLimiter;
vertical *= moveLimiter;
}
body.velocity = new Vector2(horizontal * Speed, vertical * Speed);
}
Thanks in advance!
Change Contact Pairs Mode to Enable Kinematic Static Pairs, them handle the collision in OnCollisionEnter2D
I guess you just want the player to stop when he hits the obstacle tile-map
Solution :
Add box Collider 2D for both player and the obstacle tile-map
If you want to detect collision try using trigger
Please look at the photo. There are two objects.
left circle object = circle colider2D + rigidbody2D(freeze Rotation Z, script for move )
Rigidbody2D rigid;
float moveX;
void Start()
{
rigid = gameObject.GetComponent<Rigidbody2D>();
}
void PlayerMove(){
moveX = Input.GetAxisRaw("Horizontal");
rigid.velocity = new Vector2(moveX * 5f, rigid.velocity.y);
}
void Update(){
PlayerMove();
}
right square object = square colider2D + rigidbody2D(freeze positionY, freeze positionX, freeze Rotation Z)
I can move the circle from side to side.
While I press the keyboard and push the circle to the right wall, gravity doesn't work.
I don't know why. I hope gravity will work even if the circle hit the wall.
How can I prevent the problem?
Looks like physics material that's applied to that rigidbody has too much friction and slows down too much when hugging the wall. Try reducing friction value on the material (you can create the physics2D material from the assets panel).
I'm six panel is a subset of a cube.
Now I want each of these panels inside the mouse when the white ball in the middle of the panel to be?
Figure 1.
![Image ][1][1]: http://i.stack.imgur.com/dxD1E.jpg
I've written this code, but the white ball in the middle of the cube is just not working.
void OnMouseEnter()
{
if (gameObject.tag.ToString() == "Panel")
{
GameObject sphere = GameObject.Find("Sphere");
sphere.transform.position = gameObject.transform.position;
sphere.transform.eulerAngles = gameObject.transform.eulerAngles;
}
}
If the goal is to place the ball on the face of the cube that the mouse cursor is hover over, this is an easy problem to solve if the center of the cube (origin) is at the geometric center, so if this is the case do the following:
Raycast against the cube (must have collider, probably box collider)
Using the hit.normal and the hit.point from the raycast result calculate the position of the white ball.
Set the position of the white ball
Example Code:
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit))
{
Vector3 ballPosition = hit.point + hit.normal * ballRadius;
ballTransform.position = ballPosition;
}
ballRadius is the radius of the ball and can be obtained by accessing the ball's sphere collider.
ballTransform is the transform of the ball.
I have a scene with a body maked with makehuman, and I need to add a simple prefab (a torus) around the arm of the body when the user touch the arm.
I tried:
Instantiate the prefab in the point where the user touch, but the prefab apear in the border of the arm.
Instantiate the prefab in the center of the arm, with this code:
float radio = hit.transform.collider.radius; // the arm has a capsuleCollider
Ray r = Camera.main.ScreenPointToRay(Input.GetTouch(0));
Vector3 origin = r.origin;
float distance = (origin - hit.point).magnitude;
RaycastHit ou;
Vector3 position = hit.point;
Ray r2 = new Ray(r.GetPoint(distance + 10f), -r.direction);
if (cc.Raycast(r2, out ou, distance + 10f))
position = (hit.point + ou.point) / 2;
Instantiate(Prefab, position, Quaternion.identity);
This try to Select the center of the arm and initialite a torus.
The second option works in some cases, but the general impression is that is the wrong way to do it.
How can I add a prefab around a collider? or, how can I modify the mesh to add a visual indicator?
This should work a lot better as well as look a lot cleaner:
Vector3 center = hit.transform.collider.bounds.center;
Instantiate(Prefab, center, Quaternion.identity);
hit.transform.collider is a vital part of this process and you got that part. collider.bounds is the bounding box that surrounds the collider (http://docs.unity3d.com/ScriptReference/Collider-bounds.html), and bounds.center is the center of the bounding box (http://docs.unity3d.com/ScriptReference/Bounds-center.html). The Vector3 that bounds.center returns is where you want to spawn your prefab.
From there, you should be able to rotate the prefab to the desired angle and perform any number of operations you want.
I have this cup that contains balls inside. All 2D
I am using Rigidbody2d and Collider2d.
When running in unity and moving the cup (with arrow keys) the balls stay inside the cup. I also added drag movement for Android touch to move the cup.
The problem is that when moving the cup too fast (by draging) the balls fall from the cup collider (using Polygon colider 2d).
Code for movement is:
public float speed = 0.1F;
void Update() {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
}
}
I tried to play with the speed parameter but it wont really help. if the cup movement is too slow it is not very useful for me.
I believe it is related the velocity/force of the ball or cup which makes the cup collider miss...
Any help on this would be appreciated greatly!
it is because you are moving it by changing the position of the cup, that means that when you move it fast, it disappears, and reappears where you dragged it, leaving the balls behind, and then they fall. I had the same thing where my objects would just go through the walls and out of the camera area. I fixed that by using AddForce. This makes the cup move, position by position over to where you dragged. this bumps the balls along, and they stay in the cup. This is what I guess is going on. If you use velocity that would work to.
you could rather than transform.Translate, you could find the vector to where you want to move to.
var direction = touchDeltaPosition - transform.position;
gameObject.rigidbody2D.velocity.x = direction.x * speed;
gameObject.rigidbody2D.velocity.y = direction.y * speed;
hope this solves it
The problem is that, when you're moving the cup too fast, at Frame 1 the ball is inside the cup, but at Frame 2 it's outside. You can reduce the value of "Fixed Timestep" (http://docs.unity3d.com/Manual/class-TimeManager.html) to increase the frequency of the physics calculations, or make the colliders larger.
If a ball in the cup will always stay in the cup, maybe you can turn off the physics of the ball once it's in the cup, or something along those lines.
Usually transform.Translate() is not very efficient when dealing with colliders, instead, you can try three other solutions:
1) rigidbody2D.AddForce(Vector2.Up * Input.GetAxis("Vertical"));
2) rigidbody2D.velocity = new Vector2(//write what you need);
3) which I consider the best solution for dealing with colliders and dragging is : MoveRigidbody2D
Well, this is what worked for me:
var newVec = new Vector2 (transform.position.x, transform.position.y);
var touchDeltaPosition = Input.GetTouch(0).deltaPosition*touchSpeed;
var direction = touchDeltaPosition- newVec;
rigidbody2D.AddForce(direction);