Raycasting an instantly moved collider - unity3d

I need to move some transforms with attached colliders to a specific position, then check if one of them is hit by raycast.
I've done that the naive way (pseudo code) :
foreach(object in objects){
actual_position = object.transform.position
object.transform.position = object.new_position
}
if(Physics.Raycast(...)) objectHit();
// Then I revert each of them them back to their actual_position
After testing multiple times with the same context (same positions between tests for each objects), the raycast sometimes miss, sometimes not (~50/50).
Done a bit of research and found that in the Raycast doc page :
If you move colliders from scripting or by animation, there needs to
be at least one FixedUpdate executed so that the physics library can
update its data structures, before a Raycast will hit the collider at
it's new position.
So I calmed my anger and started looking for a solution.
This thread has a way using a coroutine to wait the next tick :
Raycast doesn't work properly
I'm affraid it won't work for me, as I need the objects to get back to their real position instantly.
This process can happen multiple times per frame (each time a player fire his weapon)
Is there a way to force the colliders update ?
If not... should I make my own raycasts and colliders ? :(
Thanks

Another workaround is to deactivate and activate Gameobject (attached collider) immediately. In this case collider position will be updated in single frame.
Another solution is
Physics.autoSyncTransforms and Physics.SyncTransform

Maybe you can try this out (adding to your pseudo-code):
foreach(object in objects)
{
actual_position = object.transform.position;
object.transform.position = object.new_position;
StartCoroutine(CheckToRevertOnNextFixedUpdate(object, actual_position));
}
IEnumerator CheckToRevertOnNextFixedUpdate(object, actual_position)
{
yield return new WaitForFixedUpdate();
if(Physics.Raycast(...)) objectHit();
// Then I revert each of them them back to their actual_position
}
Essentially this delays your check to the next FixedUpdate() for each object - and reverts each of them if needed. I hope this is not overcomplicated, since you only add a few lines of code.
I'm also assuming that moving the object's position for 1 FixedUpdate() frame would not have a visual effect of the the object teleporting to the new position and back. However, you can always move the collider only, and then move the rest of the transform there after the FixedUpdate().

Performance-wise, the best method seems to be updating the RigidBody.position:
private Rigidbody Rigidbody;
void Start()
{
Rigidbody = gameObject.GetComponent<Rigidbody>();
}
void Upate()
{
//.... your code
Rigidbody.position = newPosition;
}
Much faster then deactivate/activate or Physics.SyncTransform().

Related

Continuous collision detection (CCD) in particle system in Unity, is that possible?

According to this article is possible to solve collision problems with fast moving objects in unity.
If you have particle system firing 1 sphere particle (ball throw for example) with collider and gravity modifier. And another moving collider with rigidbody (baseball bat for example), is there any possibility to make these object to NOT going through when they will collide in "high speed"? You cannot set "collision detection" on particle.
For better understainding I am adding two screenshots bellow.
Feel free for asking any question.
Collider(wall) is not moving, particle will bounce
Collider(wall) is moving towards ball with speed of "swing", particle goes through
The problem is because your objects are moving at a speed that they go through the collider. Say your object would move 10 units a frame, and it was 1 unit away from the collider. If the collider was 5 units long, it wouldn’t stop the moving object. This is because the next update will push it too far ahead of the next object.
First, don’t use a particle system for this. Just use a Rigidbody, because it is easier to handle in these situations.
You could solve this by doing a sweep test. A sweep test checks for colliders, where the object is going. You could just stop it from moving at the point it hit.
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
Sweep();
}
void Sweep()
{
RaycastHit hit;
if (Physics.SweepTest(rb.velocity, out hit, rb.velocity.magnitude))
{
rb.velocity = rb.velocity.normalized * hit.distance;
}
}
Make sure this script is on your ‘baseball’, and add force somewhere in this script. Make sure that you are not using particle system, but you are using Rigidbody.
This was untested; let me know if it doesn’t work or you get an error.

Unity 3D - Collisions

I have a problem with physics. It is my first time doing in 3D, so it may be just a beginner mistake.
I just wanted to create a simple player controller and make it so that it can not pass trough cubes.
The problem is that when going straight into the cube, part of the player is in the cube itself. When stop moving, it pushes me, so they are not intersecting (that makes sense).
I tried moving the player using .Transalte, .MovePosition and by changing the velocity of rigidbody itself. None of it change anything. The player can always move a part of him into the cube.
Any ideas how to solve this?
My player controller:
(The 2 lines commented out in Move() are just other ways to move the player.)
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField]
private float movementSpeed;
private Vector3 input;
private void Update()
{
GetInput();
}
private void FixedUpdate()
{
Move();
}
private void GetInput()
{
float inputHorizontal = Input.GetAxisRaw("Horizontal");
float intputVertical = Input.GetAxisRaw("Vertical");
input = Vector3.ClampMagnitude(new Vector3(inputHorizontal, 0, intputVertical), 1);
}
private void Move()
{
GetComponent<Rigidbody>().velocity += input * movementSpeed;
//GetComponent<Rigidbody>().MovePosition(GetComponent<Rigidbody>().position + input * movementSpeed * Time.deltaTime);
//transform.Translate(input * movementSpeed * Time.deltaTime, Space.World);
}
}
Player is standing still
Player is moving towards cube
Settings of the Game Objects itself
Now I think I understand your problem.
The collider is a geometric shape that is checked but the outcome wont take place until the collision has actually taken place, this means, one geometric shape being inside the other. By this I mean, that what you are experiencing is the normal behaviour of the collision. If both elemnts are rigid bodies, both will move and your problem wont be perceivable, but if your cube is not a rigid body or is kinematic, will stand still in the same position, and depending on the other object speed, its normal that an invasion/superposition of the elements is perceivable, because that is the frame were the collision took place, and were your element needs to be moved back because it has collided.
Consider that if the speed is high enough, and the position from one frame to another varies enough, the collision might not even take place, because the geometric parts do not interfere between frames, as the position variation might be bigger than the bounds of the collider itself. The collision check at the end of the day, is dicrete, and not continuous (even you can set it as continuous to be as frecuent as possible).
To solve or improve that, you can adjust the speeds to avoid that being perceivable + adjust your collider to make it react before the graphic superposition occurs. This means making the capsule or the cube collider bigger than the graphic element itself. You can even calculate , to make it as bigger as much as your your speed * Time.deltaTime result or speed * FixedTimeStep result, depending on your safety distance needs. I think one of those should be the safety distance you need to take into account before the graphic collision occurs.
Another thing you can do is tight more the times physics calculations are done.
https://docs.unity3d.com/Manual/class-TimeManager.html
But need to be careful with this as this can be a performance devourer and need to be handled with care. You can make some trials with that and check your problem can improve.
Hope that helps
You can increase the scale of your player's collider from the Collider component attached to it. You can check how big the collider is from the editor scene view.
Edit: The issue might be that your movement or collision code is called in Update instead of FixedUpdate. When working with rigidbodies, you want to call the physics calculations inside FixedUpdate.
remove rigidbody from the cube, you can click on 'Gizmos' in the top right of the editor and make sure the colliders are at the edges of the objects.

Is there a way to break out of Update() function

So, I'm trying to make my own movement script for Unity with a Rigidbody component attached to my character, and I'm using this code inside of the update function:
// Up above, in public scope, before Start and Update functions
public float speed = 1f;
public Rigidbody body;
void Update() {
if (Input.GetKey(KeyCode.LeftArrow())) { // Left arrow key
body.AddForce(transform.forward * speed * Time.deltaTime);
}
}
My problem is, since it's inside of the Update() function, it keeps going, it keeps turning, even if I don't have the key pressed.
Does anyone know how to fix this problem? I have tried taking away Time.deltaTime but Brackeys said you use it so that it's frame rate dependent, so if you have a higher frame rate it doesn't turn faster, and same with a low frame rate, you turn slower. Thanks in advance.
It keeps turning even after releasing the key? Yep that's what AddForce will do.
After you apply a force to an object it will continue to move based on that force uless it has drag or an opposing force is applied. Go into the rigidbody to set the drag or create a script that applies an opposing force once you stop pressing the button.
If you put a Debug.Log within the if statement you will see it is not called every frame but only when you have the button held down.
And last, I don't think that your keycode should have () after LeftArrow, does this not produce an error?
An object in motion stays in motion with the same speed and in the
same direction unless acted upon by an unbalanced force.
---- Isaac Newton
So if you want to stop the object, you can do:
Add an oppsite force
if (Input.GetKey(KeyCode.LeftArrow())) {
...
} else if (!body.IsSleeping()) {
body.AddForce(-body.velocity * factor);
}
Give a drag value
body.drag = dragValue;
You have two questions, one on the title and another one on the description.
For the title one:
The only way to stop/break Update() cycle is to disable the script, like this:
enabled = false;
For the description one:
You have to stop the force applied to the object (as the rest of the answers exemplify).
You can apply an opposite force, or just set velocity to 0.
rigidbody.velocity = Vector3.zero;

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.

unity3d 4.3 bullets coming back down

I am trying to build a 2D space shooter game where the player will remain constant at the bottom of the screen and will move left and right. When the player fires a bullet(projectile), the bullet is going up just fine, but it is coming back again. Also if two bullets collide, they react to the collision.
I have two questions here:
How can I ensure that the bullet does not come back? (One way is to destroy it after a few seconds, but not sure if this is the right way)
How do I avoid collision between the bullets.
Here is my code snippet:
void Update () {
if (Input.GetButtonDown("Fire1"))
{
Rigidbody2D clone = (Rigidbody2D)Instantiate(bullet, transform.position + transform.up, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3.up * 20);
}
}
I am very new to Unity and somehow wrote this code by looking into Unity Forums.Any help will be very much appreciated.
Thanks
More details:
I have created the bullet and added Rigidbody2D and a BoxCollider2D for this object. Made the bullet object a prefab and I dragged it under the Player object. Now bullet is a child of the Player object. Attached the script above to the Player object.
To answer your immediate questions:
1) I am not too sure about how you have the scene setup or about using the Unity 2D tools but usually if your bullets are coming back down then I would assume that you still have gravity applied to the rigidbody -- so make sure that you have that unchecked.
2) With Unity3D for objects to interact with each other they need rigidbodies attached. This is vital, say, if you want your lasers to destroy Monster #1. However, you only need one rigidbody attached to an object to have a desired effect. I would suggest removing the rigidbody from your laser and attach rigidbodies to objects ( Monster #1 ) that you want to be affected by the laser fire. ( That should work, if not there are other options -- one using layers and ignoring particular objects on those layers ).
Tips:
Here are some extra things. When you are creating objects and they fly off screen over time they will build up. Imagine you are in a large battle and you instantiated hundreds of lasers -- it will eventually be an issue for performance. So always consider Destroying objects after a certain amount of time.
Also, after looking at your code, I can't tell whether the velocity of the object is based upon its current position and nothing is being added. What I think is happening is when you instantiate an object it may be moving up, but because there is no incrementer, the object ( as it gets higher ) slows until it is at a rate which it is falling. For example this
clone.velocity = transform.TransformDirection(Vector3.up * 20);
should probably do this:
clone.velocity += transform.TransformDirection(Vector3.up * 20);
Heres http://docs.unity3d.com/Documentation/ScriptReference/Vector3-up.html, to read up on velocity. The problem is is that you need to continuously apply a constant forward motion so things in motion will tend to stay in motion. If not gravity will pull it down.
Also, heres a bit of code that I've used in the past that's created a pretty cool shooting laser effect thing:
public class Laser : MonoBehaviour {
public int fireRate = 70;
void Start(){
InvokeRepeating("FireLaser", 0.01f, 0.009f);
Destroy( gameObject, 2.0f );
}
void FireLaser(){
transform.Translate( Vector3.up * fireRate * Time.deltaTime );
}
}
edit: it's 3 a.m. and I hate proofreading.