Raycast not detecting object - unity3d

I'm new to unity and still learning. I have an object (a tile) at location (-35, 0, -35) and a second object at (-35, 1, -35). I am firing a raycast from the tile upwards towards the second object (from the tile) using the below code and logging it to see the result. The unfortunate thing is that it is not detecting it. This is the code:
Vector3 upward= transform.TransformDirection(Vector3.up);
bool test = Physics.Raycast(transform.position, upward, 2);
Debug.Log(test);
unfortunatly though, the output always returns false. Is there something I am not understanding?
Thanks,

Because of the collider that i had on the object, the raycast started inside the mesh collider and was not detecting it because it was not hitting the collider, only exiting it.

Related

Why does the object get stuck in the wall when repelled?

I made the endless movement of the object and its repulsion from the walls, but it does not always work correctly. At rounded corners (sometimes even at a straight wall), it just gets stuck and stops moving altogether, or moves slowly to the point where it stops moving. What can this be related to and how can it be fixed?
private void FixedUpdate() {
rb.velocity = direction * normalSpeed;
lastDirection = direction;
}
private void OnCollisionEnter2D(Collision2D collision) {
//Repulsion from objects.
direction = Vector3.Reflect(lastDirection.normalized, collision.GetContact(0).normal);
}
There is a small distance between the objects, but the circle seems to stick to the wall and moves with it until it collides with another collider:
Example
there are objects under the circle that also have colliders, but the collision between them is not considered, since they have the same layer (in the settings, I disabled the collision for objects on the same layer). What can be done to fix this error and what can it be related to?
The object in the general scale:
Example
I tried to increase the size of the wall collider, tried to change polygon collider to box collider, connect composite collider, changed the mechanics of the object movement (in these cases, the movement could work incorrectly), but the result was always the same - the jams (sticking to the wall) continued.
It is difficult to answer your question without having more information, but I can suggest if you are using physics to move your objects - utilize physic materials and remove code that changes direction manually in OnCollisionEnter. Using physic materials you can easily make your object bounce from colliders of your choice without losing velocity.

Unity 2D physics is not colliding when player moves fast

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.

Unity collision not being detected

I am learning to create a simple fps game in unity the problem is that the collision does not update itself for example initially when my player is on the ground console prints "floor" by "Debug.log(collision.gameObject)" but when it intersects other objects such as a cube console will print out "cube" but when I walk away from it , console does not change back to "floor" Why????
I am using transform.translate to move and jump and using method OnCollisionEnter for collision detection
OnCollisionEnter is triggered only when object enters the collider.
A) Make a list of all encountered objects by adding them when OnCollisionEnter happens and removing when OnCollisionExit happens. Then whenever you need to make sure you are on "floor" check it in the list.
B) Use OnCollisionStay and every frame you will be notified if you are touching the "floor".
Remember one thing, the other object you want to collide with need to have a collider component asigned, make sure of it. Join this with the previous answer.
I recommend to verify collision.
Here on simple example:
void OnCollisionEnter (Collision col){
if (col.gameObject){
Debug.Log("Object name : "+ col.gameObject.name);
}
}

Unity 2D Bounce Back moving object when colliding with another object

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

Unity Raycast2D results are not as expected?

I'm currently working on a practice project which involves stacking of blocks. It is sort of similar to the game of stackers (the arcade game), except my version uses free-form movement instead of grid-based movement.
I have 3 prefabs for the blocks: SingleBlock, DoubleBlock, TripleBlock.
I structured each of them like this: the parent is an empty gameobject with my MovementScript that moves them left/right, while the children are the block sprite with a BoxCollider2D.
The MovementScript is attached to the empty game object(the parent) so that the block set moves uniformly left/right, for what it's worth.
For the actual stacking logic, I'm using Raycast2D to detect if there is a block below. But the problem is the results I get is unexpected.
Here is a snippet of my current code:
foreach(Transform t in currentBlockSet.transform)
{
// get all the children of this blockset. to do this, we use the transform beause it is IEnumerable
GameObject block = t.gameObject;
RaycastHit2D hit = Physics2D.Raycast(block.transform.position, Vector3.down, rayCastLength); // 0.5f raycast length
//Debug.DrawRay(block.transform.position, Vector3.down * rayCastLength);
if(hit.collider != null)
{
// this means there is a block below, we hit something
Debug.Log("True");
}
else
{
Debug.Log("False");
}
}
This code is called each time the player stops the current blockset that is moving by the way.
The problem is I always get true in my logs even though I purposely didn't align the block set properly. I never get false even if I'm way off with my alignment. Why is this so?
I do like to mention that there's nothing else in the scene. It is just the blocks, so there can't be another object to collide with.
Is there something wrong with the logic or how I'm using Raycast2D?
Appreciate any help.
I believe what is happening is that they Raycast2D is detecting the block it is shooting from. Since you are using the block's transform.position as the origin of the Raycast, this means that the Raycast will be shooting from the center of the block.
To test this, I slightly modified your code so it would fire a Raycast in the same way, but instead of logging just "True" I logged the name of the object that was hit.
void Update ()
{
RaycastHit2D hit = Physics2D.Raycast(this.transform.position, Vector3.down, 0.5f); // 0.5f raycast length
if (hit.collider != null)
{
Debug.Log(hit.collider.gameObject.name);
}
else
{
Debug.Log("False");
}
When run, the object that was detected by the Raycast, as expected, was the object the Raycast was originating from.
To solve this issue, I would suggest adding an empty child GameObject to your blocks called "RaycastOrigin". Position this GameObject just underneath the block such that it is outside of the block's box collider. Then you fire your Raycast from the "RaycastOrigin" instead of the blocks transform.position That way, the Raycast would not hit the block its shooting from, but rather a block below it.