A 2D circle shaped player has the following code
Vector2 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = target;
which sets the player's position to the mouse position, and when it collides with enemies they get destroyed (simple concept).
The problem is that the enemies do not recognize collision when player passes through them quickly so they are not destroyed. This problem frustrated me I don't know why collision doesn't work when an object passes through another object quickly .
Is there a solution? Or it is just UnityEngine Maximum Performance
Thanks in advance.
I recommend you to see this link.
It explains much about Collision in unity.It looks similar to your game also.
You could also calculate distance between two objects and when the
distance is equal to zero you may destroy that object too.
Note1: this destroys the object only when it is coinciding with another object.
Or by calculating the outer distance between two objects when it is
equal to zero you destroy that object.
Note2: this destroys the object when two objects are starting to collide.
Related
player and object both have colliders and rigidbodies, object has position and rotation locked, player has only rotation locked. When the player goes to the blocks, the player goes through the blocks, although they do give a bit of resistance. To move the player im setting the rigidbody's velocity, and doing that in FixedUpdate.
i have no idea why this is happening, any ideas?
main part of the code is:
rigidBody.velocity = new Vector3(direction.x, rigidBody.velocity.y + (-Gravity * Time.deltaTime), direction.z);
(direction is determined by the WASD keys, and i'm using my own gravity)
First of all, you do not need to multiply the velocity by time.DeltaTime, because you are moving your object in the FixedUpdate() method; Which uses fixed time intervals since the physics engine does not run in sync with the regular game engine. Also, both objects do not need rigidbodies in order to collide with one another. I suggest looking at your collision matrix in settings and verifying that everything you need collision for is checked correctly. As others have said as well, check your kinematics on the rigidbody.
A last suggestion for working with your own gravity. Do not change the actual gravity value of the game engine. It is typically recommended that you use a multiplier variable and apply it to the constant gravity already set by the physics engine. If you are completely editing the gravity, than maybe consider using a character controller instead.
I guess it has something to do with what the documentation says "In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour".
Try to use AddForce() or similar functions to alter the properties of the rigid body. Colliders etc will then work as expected.
Im trying to make a game in Unity and I want to have a gameObject fall and match the shape of the ground underneath it.
when the game is started:
what is does:
what I would like it to do
I'm pretty decent so even a hint in the right direction would be helpful.
thanks.
One way to do it is to duplicate the ground object and replace the falling object with ta modified version of that duplicate. How you can do that is to do the following on entering a collision between the falling object and a given ground object:
Make sure this falling object hasn't already collided with this ground object, such as with a fallingObject.lastCopiedGroundObject GameObject.
Find the y of groundObject's Collider.bounds.size
Add y to groundObject.transform.position to a new Vector3 newPos
Use GameObject newFalling = Object.Instantiate(groundObject, newPos, groundObject.rotation, fallingObject.parent).
Copy rotationalVelocity and velocity from the fallingObject's rigidbody over to newFalling's rigidbody, if appropriate.
Add, remove, modify components as necessary on newFalling, (such as replacing textures) to make it further behave and appear correctly as the falling object it is replacing.
Destroy the old falling object with fallingObject.Destroy()
Set the lastCopiedGroundObject of newFalling so that newFalling won't do this interaction with the same groundObject again, until another copy is done.
This will place the falling object above the ground object, and will let it fall using the physics engine to determine where it should rest on top.
One consideration is to instead use a List<GameObject> alreadyCopiedGroundObjects and to append the newly collided ground object to the list in the old falling object so that if the falling object touches multiple grounds, it will only change its shape to each one only once, so it won't perpetually oscillate between shapes in certain conditions.
I want to set transparent all trees which are between player and camera, my game is top down, and vector between camera and player changes. So, how to Raycast between two points and also get all objects that are hit by ray? I know there is Linecast for raycast between two points, but it returns only first object and RaycastAll on the other hand can be casted only in specific direction... Any idea how to cast ray between player and camera and get all hit objects?
Although Physics.RaycastAll() doesn't appear to immediately meet your needs, you can easily adapt it to give you what you want.
If you perform a raycast from the player in the direction of the camera, and limit it to only the distance between the player and the camera, then you effectively only cast a ray between the two positions and will only get object between them.
Here's how I suggest you approach it:
float distToCamera = Vector3.Distance(camera.transform.position, player.transform.position);
Vector3 dirToCamera = camera.transform.position - player.transform.position;
RaycastHit[] hits;
hits = Physics.RaycastAll(player.transform.position, dirToCamera, distToCamera);
Hope this helps! Let me know if you have any questions.
A quick search and look at this and use it on your trees and when they became visible to the camera and after that do what ever you want with objects
note : this event can be fire with any camera rendering those objects so beware of which camera you are using to render trees is right
I have an object that, after receiving its respective input, it moves this way:
mov = new Vector3((Input.GetAxis("Horizontal") * vel), 0, 0);
transform.position += mov;
But, I want it to bounce back, once it collides with an object.
I´ve made the procedures already (OnCollisionEnter2D(Collsion2D col){bla bla...}), but I need help with what happens on the collision (bouncing back the object)....
I´ve tried giving the collided object a bouncing material, but it just slows it a bit, my guess is that because of the constant force given by the acceleration.
Greetings.
If you move the object with transform.position what you are doing is basically a "teleport" so it will ignore the bouncing material. If you want it to bounce you have to write the physics code to detect a collision and change the movement or you can do addforce to move the object and it will detect collisions and react automatically.
you are teleporting the object at the current time. instead you should use the Rigidbody.addForce this will add a force in the specified direction thus if you do the opposite direction will "bounce" of the object. Another option would be to create a physics material then not bother with the code.
You are not using materials, right?
See if the content of this post may help you, the OP is using a formula using Raycast and the answer guides him to use the Raycast with Layers Maks:
2D bouncing formula doesn't work properly
There is this one also with fixed angles (like Pong), but it uses material (with values: friction: 0, bounciness: 1):
https://gamedev.stackexchange.com/questions/70294/get-gameobject-to-bounce-of-colliders
But if nothing makes sense and you are going crazy and might want to start from zero, there is this official video tutorial on bouncing and sliding in 2D:
https://unity3d.com/learn/tutorials/modules/beginner/2d/sliding-bouncing-2d
Imagine you have a ball falling due to gravity. When it encounters a rotating "cube" object, then you would expect it to bounce off of the object. However, in my case if the cube is rotating fast, the ball goes through it, but if the cube is rotating slowly, the ball hits it and bounces away as expected.
I am using RotateAround() inside the Update() method to achieve the "cube" object's rotation. I tried setting the ball's collision detection to Discrete, Continuous, and Continuous Dynamic with no luck.
My goal is to make the ball bounce away no matter how fast the "cube" object is rotating around another object.
Is there something else I should look into?
Thanks!
You can try lowering the Fixed Timestep value under Edit > Project Settings > Time.
Be aware that this will affect the performance of the game as you're calculating physics more often.
Documentation: http://docs.unity3d.com/Manual/class-TimeManager.html
Also, I assume that you have box and sphere colliders as opposed to mesh colliders? The former are more efficient at detecting collisions.