I'm really struggling at something that I had imagined to be pretty simple: I have two meshes (fighters) instantiated from the same prefab and I want them to not overlap. The prefab is set to have a box collider. Ideally, I let the animator handles the position. I've tried several approaches:
Configure the animators to "Animate Physics". No collision is detected
Uncheck root motion and move characters using game object's transform's position. No collision is detected
Uncheck root motion, add a rigid body to each character and move character using game object's position. Collision is detected, but the reaction is governed by physics which makes it look unnatural since all I want is to just avoid the characters to pass through each other i.e. just want characters to be pushed back until they don't collide anymore
I really would like to avoid going with a manual approach to this. How would I prevent this overlapping?
Edit: isTrigger is unset on box colliders
A rigidbody is highly configurable and you want a limited set of the physics functionality it can offer. Go with the third solution you enumerated and freeze the rotation in the rigidbody. See the rigidbody page for more details.
As a side note, you might consider using a capsule collider, so that characters "slide" around each other more easily.
Although it's not necessarily preventing overlap, you could create a perimeter for characters. It would be rather simple depending on what you're doing.
If you want to set up a distance perimeter to prevent characters from getting too close to one another you could use the following equation
Vector3 focalPOne = focalPointOfCharacterOne();
Vector3 focalPTwo = focalPointOfCharacterTwo();
float dx = focalPOne.x - focalPTwo.x;
float dy = focalPOne.y - focalPTwo.y;
float dz = focalPOne.z - focalPTwo.z;
// calc distance along z plane
if (Math.Sqrt((dx * dx) + (dy * dy)) < minDistance)
{
// person is too close to the others head.
if (Math.abs(dz) < minHeightAbove)
{
// go to previous position.
}
// if there is no vertical elivation, you can go
// straight to moving them back to previous position
}
enter code here
If this wasn't what you're looking for, please let me know so I can edit or remove it.
The perimeter created would look something like this.
In order to have your characters with their respective box colliders register a collision, both need to have a RigidBody component. That's the only way to make sure the characters do not go through each other considering they share a prefab.
You don't have to handle the separation/rejection of the characters via the physics engine though. Simply make the box collider trigger, and then handle the separation via code in a C# script using onTiggerEnter.
There's a collision detection matrix that could help you, found here: https://docs.unity3d.com/Manual/CollidersOverview.html. Also this image summarizes it beautifully (in the documentation it's in two separate tables for some reason):
Related
I have 3d cube which have 3d box collider.
I want to write jump system for cube with collision, but without physics (without gravity, without rotation, etc) (Like 3d platformer)
You can take advantage of the Physics.OverlapBox API to check for collisions before committing to move your object.
Your code will look something like this:
Vector3 nextPosition = transform.position;
/*
You do your normal movement code, but apply
it to nextPosition instead of transform.position
*/
Collider[] hitColliders = Physics.OverlapBox(nextPosition, transform.localScale/2);
if(hitColliders.Length == 0){
transform.position = nextPosition;
}
Note that the object itself shouldn't have a collider, otherwise it will detect itself. You also shouldn't move in big steps as this method doesn't take colliders on the path into account.
There is a great way of doing this. You will still need physics.
The Steps
Have a rigidbody on your player.
There should be a dropdown menu called spmethiing like "constraints". Find it.
Then Set all rotation constraints in this menu to true.
However, I don't understand why you wouldn't want gravity. Don't all platformers have gravity? (There must be some force to pull your player down when they jump). Anyways, if you don't want gravity, then you must uncheck Use Gravity variable.
**Notes: **
To control your player, you must use AddForce(). If you have any questions about adding force, let me know in the comments.
:) Thanks.
I don't want my player to be able to walk off ledges. I did this by shooting a single raycast downwards in front of the player, and if ground is NOT hit, then ignore input.
However this is jarring, especially if you diagonally walk along an edge you just completely stop, rather than 'slide' along it.
So I thought I could add two raycasts, one per side to detect which side the ledge is, then allow movement (or steer) the player as applicable.
The problem is I'm not sure how to proceed from here. I'm using a character controller for movement, my current code is like:
velocityXZ = velocity;
velocityXZ.y = 0; // we deal with gravity elsewhere
velocityXZ = inputDir * playerSpeed;
if (facingDropLeft || facingDropRight) {
velocityXZ.x = 0;
velocityXZ.z = 0;
}
velocity = new Vector3(velocityXZ.x, velocity.y, velocityXZ.z);
// handle gravity
charController.Move(velocity * Time.deltaTime);
Could anyone offer some insights into what direction to look into, or methods I will need?
I think that if you want to accomplish an enjoyable result you should use invisible walls. I think that probuilder can help you.
This is the approach I would have with this type of problem.
Use boxes to make wall than turn off the mesh renderers this will make invisible walls
I'm trying to make a character that walks around a platform and if the character reach a corner, it rotates and continue walking on the side of the platform, same with the bottom part.
This is a visual representation of what I'm trying to achive.
Movement
The specific problem is when the character reach the corners, the rotation just go crazy. I'm trayng to achive this using a raycast from the character to the platform and if the raycast doesn't find floor, I start the rotation like this:
times++;
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.AngleAxis(times*-89.9f,Vector3.forward),Time.deltaTime * RotationSpeed);
_characterGravity.SetGravityAngle(transform.localEulerAngles.z);
I'm using a characterGravity script that allows me to change the gravity direction for the character in order to not fall when is walking upside down or on the sides. But this is not working propperly. Is there a better way to do this?
Assuming your 2D view is aligned on the X-Y plane, with the Z-axis aimed into the screen (the same direction the camera is facing), I suggest using Transform.Rotate() instead of trying to linearly interpolate the rotation between two values:
if (ShouldRotateAroundCorner()) {
transform.Rotate(Vector3.forward, RotationSpeed * Time.deltaTime);
}
You'll just need to make sure your ShouldRotateAroundCorner() knows when to start and stop the 90 degree turn, which will take a little bit of additional code to keep track of when it's in this state change.
I'm going through a few different Unity tutorials and the way a game object is moved around in each is a little different.
What are the pros/cons to each of these methods and which is preferred for a first person RPG?
// Here I use MovePosition function on the rigid body of this component
Rigidbody.MovePosition(m_Rigidbody.position + movement);
//Here I apply force to the rigid body and am able to choose force mode
Rigidbody.AddForce(15 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
// Here I directly change a transforms position value, in this case the cam
Transform.transform.position = playerTransform.position + cameraOffset;
Thanks!!
EDIT;
Something I have noticed is that the applied force seems to memic wheeled vehicles while the position changes memic walking/running.
RigidBodies and Velocities/Physics
The only time, I personally have used the rigidbodys system was when implementing my own boids (flocking behaviour) as you need to calculate a few separate vectors and apply them all to the unit.
Rigidbody.MovePosition(m_Rigidbody.position + movement);
This calculates a movement vector towards a target for you using the physics system, so the object's velocity and movement can still be affected by drag, angular drag and so on.
This particular function is a wrapper around Rigidbody.AddForce I believe.
Pros :
Good if realistic physical reactions is something you are going for
Cons:
A bit unwieldy to use if all you are trying to achieve is moving a object from point A to point B.
Sometimes an errant setting set too high somewhere (for example: Mass > 10000000) can cause really screwy bugs in behaviour that can be quite a pain to pin down and mitigate.
Notes: Rigidbodies when colliding with another Rigidbody would bounce from each other depending on physics settings.
They are also affected by gravity. Basically they try to mimic real life objects but it can be sometimes difficult to tame the objects and make them do exactly what you want.
And Rigidbody.AddForce is basically the same as above except you calculate the vector yourself.
So for example to get a vector towards a target you would do
Vector3 target = target.position - myPosition;
Rigidbody.AddForce(target * 15 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
If you don't plan on having any major physics mechanics in your game, I would suggest moving by interpolating the objects position.
As it is far easier to get things to behave how you want, unless of course you are going for physical realism!
Interpolating the units position
Pros :
Perhaps a little strange to understand at first but far simpler to make objects move how you want
Cons:
If you wanted realistic reactions to objects impacting you'd have to do a lot of the work yourself. But sometimes this is preferable to using a physics system then trying, as I've said earlier to tame it.
You would use the technique in say a Pokemon game, you don't stop in Pokemon and wait for ash to stop skidding or hit a wall and bounce uncontrollably backwards.
This particular function is setting the objects position like teleporting but you can also use this to move the character smoothly to a position. I suggest looking up 'tweens' for smoothly interpolating between variables.
//change the characters x by + 1 every tick,
Transform.transform.position.x += 1f;
Rigidbody.MovePosition(m_Rigidbody.position + movement);
From the docs:
If Rigidbody interpolation is enabled on the Rigidbody, calling Rigidbody.MovePosition results in a smooth transition between the two positions in any intermediate frames rendered. This should be used if you want to continuously move a rigidbody in each FixedUpdate.
https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html
Rigidbody.AddForce(15 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
This will make the object accelerate, so it won't travel at a constant velocity (this is because of Newton's second law, Force=mass*acceleration). Also if you have another force going in the opposite direction this force could get cancelled out and the object won't move at all.
Transform.transform.position = playerTransform.position + cameraOffset;
This will teleport the object. No smooth transition, no interaction with any forces already in the game, just an instant change in position.
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