Using Unity Physics to consistently bounce a ball to the same height - unity3d

My ball can only bounce on the y axis. X and Z movement as well as all rotation are locked in the rigidbody component. Originally I was planning to handle this using a combination of the two methods below. I'm adding my own faux-gravity to the object in FixedUpdate and bouncing the ball upwards when it collides with a platform in OnCollisionEnter. Esentially sending it bouncing up and down forever.
void OnCollisionEnter(Collision collision)
{
ball.AddForce(Vector3.up * force, ForceMode.Impulse);
}
void FixedUpdate()
{
Vector3 gravity = globalGravity * Vector3.up;
ball.AddForce(gravity, ForceMode.Acceleration);
}
The problem is that if I place an angled platform under the ball, it no longer bounces vertically with the same force. I believe this is because Unity Physics is calculating the angle/force the object should bounce in a 3D space, and then because of my script only applying the upward force of that calculation. However, I'd like my ball to bounce with a consistent vertical force regardless of the angle of the platform placed under it.
This could be achieved using a coroutine and Lerp to a certain height, which I tried using the script below, using Physics gravity this time, but it didn't have the same natural bouncing feel as Unity Physics. I'd like to go back to using the Physics system but I don't know how to stop Unity from doing the angular calculation and just launch my ball vertically with the force I want.
IEnumerator MoveBall(Vector2 newPos, float time)
{
ball.useGravity = false;
float elapsedTime = 0;
Vector2 startingPos = transform.position;
while (elapsedTime < time)
{
transform.position = Vector2.Lerp(startingPos, newPos, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield return null;
}
ball.useGravity = true;
}

It sounds like you want your collider to be setup as a trigger. On your ball's collider, set isTrigger to true and then use OnTriggerEnter instead of OnCollisionEnter. This will prevent Unity's physics engine from creating and resolving a Collision between the ball and whatever it hits.
If you'd also want the ball to bounce off other objects in way that uses Unity's Physics, you'll need to then get creative with which colliders are/arent' triggers, and which physics layers they belong to.

Related

Unity2D: Making kinematic player collide with tilemap collider

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

Unity: How to find a empty hole in a wall for player?

I'm trying to figure out simple custom 2D physics for platformer. At the moment I'm using ray casts to figure out collision between the player and map. However using ray cast has some problems. For example, if the player is falling (has somewhat high downwards velocity) its unable to pick up holes in the wall because it moves past them due to going too far down to be detected as empty space.
One solution is to move to tile based system instead of using ray casts but I would preferably not to do so.
So I'm wondering is there some kinda solution to figure out empty holes in wall, even small edge ones without huge performance impact.
High quality drawn illustration, assume leftwards velocity:
Physics2D.BoxCast is how I would tackle this. It does what you expect, instead of a ray it calculates as if a full box was traversing the distance.
Usage is very similar to raycasting. Example:
public Collider2D mainCollider;
public Vector2 velocity;
void Update() {
Vector2 origin = new Vector2(transform.position.x, transform.position.y);
Vector2 size = mainCollider.bounds.size;
Vector2 direction = velocity * Time.deltaTime;
float maxDist = velocity.magnitude;
var hit = Physics2D.BoxCast(origin, size, 0, direction, maxDepth);
if (hit == null) {
transform.position += direction;
} else {
transform.position += direction.normalized * hit.distance;
}
}
If boxes are not your taste, there's also a Physics2D.CapsuleCast and Physics2D.CircleCast.

How to add inertia to object 3d in Unity?

I create a game and I need use inertia for object.
Example:
The image shows all what I need.
When I touch on screen, blueObject no longer uses the position of brownObject and rotation of redObject. And I add component Rigidbody. The object just falls down. I need him to fall further along his trajectory (inertia).
I tried to use addForce(transform.forward * float), this not work.
By setting the position of the transform, you don't use Unity Physics engine. Your cube must have a rigidbody from the begin of the simulation and what you need here is a spring joint (https://docs.unity3d.com/Manual/class-SpringJoint.html) or a fixed joint.
You need to calculate the current speed, when releasing the object.
Track the positions over the last frame & current frame, and use Time.deltaTime to compensate different frame-rates.
Then set this velocity to your objects rigidbody. (AddForce is just manipulating the velocity, but depending on the ForceMode it respects mass etc.)
public Vector3 lastPosition = Vector3.zero;
void Update()
{
// maybe do : if(lastPosition != Vector3.zero) to be sure
Vector3 obj_velocity = (lastPosition - transform.position) * Time.deltaTime;
lastPosition = transform.position;
// if you release the object, do your thing, add rigidbody, then:
rb.velocity = obj_velocity;
}
That should create the "inertia". the velocity contains the direction and the speed.

How to lock position of physic body in z axis in Unity 3D

I am developing a 2.5D game. In that game I want my character (which has Rigidbody component attached to) to just move on x and y axises. So I use this code snippet:
private void LockZAxis () {
Vector3 currentPosition = _rigidbody.position;
currentPosition.z = 0;
_rigidbody.position = currentPosition;
}
I call this LockZAxis method in the end of both Update, FixedUpdate and LateUpdate. But it doesn't work. When my character run forward for a while, its z position is still changed.
For additional information, in my code, there are two times I manipulate the position of RegidBody. The first is when my character jump, that time I use this:
jumpVelocityVector = Vector3.up * jumpForceUp + transform.forward * jumpForceForward;
_rigidbody.velocity = jumpVelocityVector;
And each frame when I want my character to move a bit faster, so in the update method, I have this:
void Update () {
Vector3 newPosition = transform.position + transform.forward * speed * Time.deltaTime;
newPosition.z = 0;
_rigidbody.MovePosition (newPosition);
LockZAxis ();
}
A rigidbody is used to simulate physics, by setting the rigidbody's position every frame you're essentially teleporting the character every frame. You can restrict movement in z-axis, this will prevent it to move in z-axis when physics is applied, which is what a rigidbody typically is used for.
Here is how to restrict rigidbody positional change:
If you run your LockZAxis() after you've changed the position it should teleport the object to the z-position of 0 every frame. Please make sure that the z-axis is the correct axis. You can debug this by pausing a running game and manipulating the Transform values to see how each axis moves your Object.
Here is how you can do it with C# Script:
Freeze All Positions
rigidbody.constraints = RigidbodyConstraints.FreezePosition;
Freeze Specific Positions:
rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
Unity Documentation
Is physics gravity set to only affect the Y position ?
Physics.gravity = new Vector3(0, -1.0F, 0);
And set these also
rigidbody.angularVelocity = Vector3.zero;
rigidbody.velocity.z=0;
make sure your rigidbody is set to kinematic since you are using Rigidbody.moveposition() and using moveposition() will directly effect velocity internally on a kinematic rigidbody
Try using moveposition() for you jump instead of velocity

Sprite character slides back when near edge of other collider

I am making a 2D game in Unity. I have added a 2D box collider and a circle collider as trigger on my sprite character. The platform on which the character is standing also have a 2D box collider. So, when my character moves near edge of platform, it experiences a force or something that pulls it away from the edge. You can think it as a protective force that helps your character not falling down the plane but the problem is that this force is not part of game and that's why it should not be there. Following is the code I am using to move my character:
// call this method to move the player
void Move(float h)
{
// reduces character velocity to zero by applying force in opposite direction
if(h==0)
{
rigidBody.AddForce(new Vector2(-rigidBody.velocity.x * 20, 0.0f));
}
// controls velocity of character
if(rigidBody.velocity.x < -topSpeed || rigidBody.velocity.x > topSpeed)
{
return;
}
Vector2 movement = new Vector2 (h, 0.0f);
rigidBody.AddForce (movement * speed * Time.deltaTime);
}
Here is image of properties.
If I keep pushing the character it will fall off the edge but if I stop just by edge the unwanted protective force pulls it back on plane.
Also, if the character bumps into another 2D box collider, it bounces back instead of just falling down.
EDIT- Bouncing effect arises mostly when player bump into other objects while jumping. Code for jumping is
void Update()
{
// The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
// If the jump button is pressed and the player is grounded then the player should jump.
if(Input.GetButtonDown("Jump") && grounded)
jump = true;
}
void Jump()
{
if (jump)
{
jump = false;
rigidBody.AddForce (Vector2.up * jumpSpeed * Time.deltaTime);
}
}
The problem lies in how you're implementing the "drag" force that slows your player down to zero. Because game physics happens in steps rather than continuously, your opposing force can overshoot and briefly cause the character to move in the opposite direction.
In this situation your best bet is to multiply your speed by some fraction each frame (likely between 0.8 and 0.95), or to use Mathf.Lerp to move your X velocity towards zero.
rigidbody.velocity += Vector2.right * rigidbody.velocity.x * -0.1f;
OR
Vector3 vel = rigidbody.velocity; //Can't modify rigidbody.velocity.x directly
vel.x = Mathf.Lerp(vel.x, 0, slowRate * Time.deltaTime);
rigidbody.velocity = vel;
Also give some thought to doing your movement/physics related code within FixedUpdate() instead of Update() and using Time.fixedDeltaTime instead of Time.deltaTime. This will allow for more consistent results independent of framerate.