Unity Ball Shaking as it moves - c#

I have a ball game object that I am making bounce using explicit physics equations. I tried using unity's physics engine, but It was not exact enough. I am setting the balls position like so:
float dTime = Time.time - timeSinceBounce;
ballPosition.x -= meterOffset / sensitivity;
ballPosition.y = ballInitialPosition.y + GameConstants.Instance.GetBallYVel ()
* dTime - (float)0.5 * GameConstants.Instance.GetGravity () * dTime * dTime; //Yi+Vy*t-0.5(G)(time^2)
ballPosition.z = (-dTime * GameConstants.Instance.GetBallZVel ()
+ ballInitialPosition.z) * startConstant; //Zi + t*Vz
ball.transform.position = ballPosition;
This code runs in the Update() method. When a collision is detected the time and initial position as reset, and this works. Currently the ball bounces properly but shakes when it is moving quickly. The FPS is at 80. I don't think my equations are wrong because it bounces properly there is just a shake. What is the best way to remove the shake? Thank!
Edit 1: I decided to write my own physics because Unity's physics engine had a lot of other components like friction and roll that were making the ball's bounces vary. Also the camera's x and y is fixed the only thing that is changing is the camera's z position. It works a lot better in fixed update

As of asker's request I hereby add all the details I can, even though without seeing the actual, full and complete code along with seeing this "shake" effect it's challenging to give an exact and proper answer. I'll try and do my best though.So let's do this.
1. "Shaking" reason: most probably you set up some kind of animation, along with a forced positioning/rotation/etc and these two codes work against each other. Double-check your AnimatorController and Animator as well as your Mecanim if you have set/configure these (also a good read: "Locomotion").You can also keep the Animator Window open while you are testing from Unity UI, to see who is the tricky guy doing the "shaking" effect for you, and get one step closer to why it happens.If these don't help, start from scratch: Minimize the code you wrote to move the ball, remove every effect, etc and add them back one by one. Sooner or later "shakieness" will come back (if it persist with the cleanest possible code, see above, Animator/Locomotion issue)
2. Think again not rewriting Unity Physics... You mention for example "...physics engine had a lot of other components like friction and roll that were making the ball's bounces vary...".Now, this is something you can turn off. Completely if you want. Easiest way is to set everything to zero on Physics Material (i.e. friction and bouncieness) but if you want Unity to ignore all and every "actual physics" and let you do what you want with your GameObject, tick in the isKinematic property of the Rigidbody. The fun part of it, you'll still collide if and where you want, can AddForce, etc yet neither gravity nor friction or "anything physical" affect you.If you are not comfy with Unity (and you are not it seems), I highly recommend learning about it a bit first as it is a very good and very powerful engine - so don't throw it's features away as they don't work as you initially expected they'll do. For example, Sebastian Lague just started a great beginner series a few weeks ago. The guy is a teacher, his videos are both short, fun and full of information so I can just recommend his channel.
3. As of your code I would make these changes:
public static class GameConstants { //no monobehaviour == can be a "proper, global" singleton!
public static float GetGravity {
get { return [whatever you want]; }
//why not Physics.gravity, using locally when and where it's needed?
}
//etc, add your methods, properties and such
//use this approach if and only IF you access these values application wide!
//otherwise, instead of a """singleton""" logic you just implemented,
//consider adding an empty GameObject to the Scene, name it "GM" and
//attach a "GameManager" script to it, having all the methods and
//properties you'll need Scene-wide! You can also consider
//using PlayerPrefs (see help link below) instead of a global static
//class, so you save memory (but will need to "reach out" for the data)
}
private void FixedUpdate() { //FIXED update as you'll work with physics!
//float dTime = Time.time - timeSinceBounce; //not needed
//positioning like this is not recommended, use AddForce or set direction and velocity instead
ballPosition.x -= meterOffset / sensitivity;
ballPosition.y = ballInitialPosition.y + GameConstants.Instance.GetBallYVel ()
* Time.fixedDeltaTime - 0.5f * GameConstants.Instance.GetGravity () * Time.fixedDeltaTime * Time.fixedDeltaTime; //Yi+Vy*t-0.5(G)(time^2)
ballPosition.z = (-dTime * GameConstants.Instance.GetBallZVel ()
+ ballInitialPosition.z) * startConstant; //Zi + t*Vz
ball.transform.position = ballPosition;
}BUT to be honest,
I would just throw that code away, implement the Bouncing Ball example Unity Tutorials offer, alter it (see point #2) and work my way up from there.
Items not linked but I mentioned in code:
PlayerPrefs, Physics.gravity and Rigidbody.velocity.
Note that 'velocity' is ignoring parts of physics (speeding up, being "dragged", etc) i.e. objects suddenly start moving with the speed you set (This is why Unity5 recommends AddForce instead -linked above-)
It might also come handy you can alter the FixedUpdate() frequency via Time Management, but I'm not sure this is yet needed (i.e. just for bouncing a ball)
I hope this helps and that ball is going to bounce and jump and dance and everything you want it to do ;)
Cheers!

Related

A-Star Unity 2D: How to find vector2 coordinates of AI's current waypoint?

[Using A-Star project] Hi. So the problem is in the title basically. I have a top-down game in which the enemy should face the direction they're going. I've tried:
To calculate Enemy's force of it's RigidBody in FixedUpdate
To calculate the vector from enemy to target.
In the first instance Enemy changes its animation states too quickly, every fixed frame there's a new force applied (especially annoying when the AI is close to target).
In the second Enemy always faces its target ignoring any obstacles. It's wallhacking, if you will.
To solve this bastard I decided to find AI's current waypoint and I do not know how to do that. I've found steeringTarget method in A-Star's documentation, but I couldn't figure out how to implement it.
I would REALLY appreciate any help. Thanks in advance!
(steeringTarget method)
https://arongranberg.com/astar/documentation/dev_4_1_0_9f8b6eb7/class_pathfinding_1_1_rich_a_i.php#a399e2bebfc8dfaf4fd291f051ca486e6
For questions related to A* Pathfinding Project it may be better to ask on the developer forum.
But here's my experience using it.
Usually you start with a seeker.
public Seeker seeker;
Since pathfinding calculations are asynchronous you must register for an event that tells us when the path is ready.
seeker.pathCallback += OnPathComplete;
Somewhere in your code you call to search for a path.
seeker.StartPath(start, end);
When the path is ready you will get a Path object. It contains all of the points and vectors.
List<GraphNode> path;
List<Vector3> vectorPath;
You can use the built-in PathInterpolator to smooth out the movement.
When you get the new path pass the vector path to the interpolator.
interpolator.SetPath(path.vectorPath);
On FixedUpdate you need to interpolate and apply the movement.
interpolator.MoveToCircleIntersection2D(transform.position, 0.1f, GraphTransform.identityTransform);
var moveDir = (interpolator.position - this.transform.position).normalized;
// Apply moveDir to Rigidbody, or whatever.
So thanks to the guy that tried to help, but that was not it. I solved my problem this way.
Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
So this is the vector that's needed. From this game object to the end of the waypoint. Your code may vary.
Vector2 force = direction * speed * Time.deltaTime;
And this is the variable that is needed. So we set the animator with force.x and force.y and this should work.
I hope I made this at least somewhat clear. But if not - ask away.

Recommendations on how to do eight directional movement

I would like to make eight directional movement in my game.
I was successful of doing the movement using rigidBody2D but it wasn't as snappy as I expected. I looked at the scripting API and there looks like alot of ways of moving things around so it's abit intimidating for a noobie like me ;^;. I tried translating but it seems that I didn't put in the right arguments since the player character didn't move(It did compile perfectly fine)
Even though my previous way of moving the character with the rigid body was fine I need snappy tight movement since the game is gonna be a bullet hell similar to Touhou so the smooth velocity based movement won't work well unless I was making a rage game(which I'm not).
I'm thinking that the solution for it is to use position based movement but the API makes finding things abit intimidating.
Edit: Here's the code that wasn't snappy but did make the character move around in eight directions
moveX = Input.GetAxis("Horizontal") * speed;
moveY = Input.GetAxis("Vertical") * speed;
movement = new Vector2(moveX, moveY);
rb.velocity = movement;
This is all inside of the update function.

How to get collision data from Physics.OverlapBox()?

I'm trying to find other methods of registering collisions (other than OnCollisionEnter() and OnCollisionExit()). I'm currently using Physics.OverlapBox(), but I need more information about the collision; i.e., normal, point.
I can use Physics.BoxCast(), but the problem is that it moves a box a given distance, and using maxDistance = 0f won't work.
I need a method of checking for collisions similar to Physics.OverlapBox() except in that it would also return information about all collisions in the cast.
Any help is appreciated. Thanks.
Your concern, expressed in the comment to the first answer is valid, but the bad news is that there is no simple trick to go around it. What you should be looking for is called continuous collision detection with a simplified version described in my answer on a somewhat similar matter:
Basically, for each moving object in your scene you have to calculate
moment of next collision within the fraction of the frame 0<t0<1,
then advance positions to this moment t0 within the frame, update
velocities due to collision and proceed further to the next collision
t0<t1<1, until you reach time of tn=1 (end of frame), making sure
you don't get stuck in a the middle of the frame due to rounding of
calculation or "cornered" objects. For spherical colliders, that is
usually done by using capsule vs capsule (for pairs of objects)
intersection and capsule vs box for the boundaries.
In oppose to the simple engine from the answer I'm referring to, Unity has continuous collision detection.
So you can enable continuous collisions and continuous dynamic which computationally is very expensive.
You can also try using RigidBody.SweepTest which returns the closest collision information. Notice that even though there is also RigidBody.SweepTestAll, it doesn't help much. Not only because it returns only first 128 collisions, but also because it doesn't process them as there is no reflection. For physically correct behaviour you have to do what described above - advance time till the first collision and update velocities. Either with the physics engine or by yourself. This is very costly and not many games are doing that even cheating by using simplified objects (spheres are the cheapest ones as two swept spheres are two capsules and their intersection is a relatively cheap calculation), but amount of steps, especially in the "cornered" case when objects have nowhere to go and therefore are constantly colliding could be very large and such cases happen more than one can expect.
For complex objects you unlikely can do better than SweepTest, unless you trigger it based on the simpler primitives, such as Physics.BoxCast or Physics.SphereCast. Again, even though there are Physics.BoxCastAll and Physics.SphereCastAll they are not particularly useful as only first collision is guaranteed to occur. Those xxxCastAll are the functions you wrote you were looking for, so give it a try, they might work well enough for your use case.
You can use OverlapBox and use Collider's ClosestPoint to select a single point of overlap, and use that to make your collision calculations.
Collider[] cols = Physics.OverlapBox(...);
Vector3 myPosition = transform.position; // for example
foreach (Collider col in cols) {
Vector3 closestPoint = col.ClosestPoint(myPosition);
Vector3 positionDifference = (closestPoint-myPosition);
Vector3 overlapDirection = positionDifference.normalized;
}
This overlapDirection will point in the direction away from the the position you use in ClosestPoint to the center of each colliding collider. If you want something based on the surface of your object, what you can do is use that overlap direction to place a raycast aimed at your object, to find the normal that way:
// ...
foreach (Collider col in cols) {
Vector3 closestPoint = col.ClosestPoint(myPosition);
Vector3 positionDifference = (closestPoint-myPosition);
Vector3 overlapDirection = positionDifference.normalized;
RaycastHit hit;
int layerMask = 1; // Set to something that will only hit your object
float raycastDistance = 10.0; // something greater than your object's largest radius,
// so that the ray doesn't start inside of your object
Vector3 rayStart = myPosition + overlapDirection * raycastDistance;
Vector3 rayDirection = -overlapDirection ;
if (Physics.Raycast(rayStart, rayDirection, out hit, Mathf.Infinity, layerMask)) {
Debug.Log(hit.normal);
Debug.Log(hit.position);
} else {
// The ray missed your object, somehow.
// Most likely it started inside your object
// or there is a mistake in the layerMask
}
}

Unity2D: implementing a running effect to my character

I'm trying to achieve the same effect done Knuckles does in the new game Sonic mania, the effect can be seen here (2:18 - 2:25). So far I duplicated my main player and lowered the duplicated players alpha so that it looks a bit transparent, I also added the script below on the duplicated player to give the duplicated player some distance to the original player; however I wasn't quite sure how I can make the duplicated players slowly return the original player when the player isn't moving! I attempt using animation but it didn't look as good as what was shown in the video, I also tired to shorten the distance over Time.deltaTime however it still didn't look effective! Is there a better way of attempting the same effect shown in the video?? Thank you :)
public GameObject Player;
public float distance = 0.75f;
// Use this for initialization
void Start () {
distance = 0.42f;
}
// Update is called once per frame
void Update () {
transform.position = (transform.position - Player.transform.position).normalized * distance + Player.transform.position;
}
You can try using TrailRenderer.
Here's the documentation: https://docs.unity3d.com/Manual/class-TrailRenderer.html
You can activate it during the run effect and deactivate it otherwise.

How to keep velocity constant when object travels at peak speed

I'm working on a project in Unity 2D for learning purposes. It's a game of Ping Pong. I have its material so it travels faster every time it bounces. The only problem with its material is that its speed gets out of hand and glitches out. I want to find out a way to stop it.
For example, I would like to know how to keep the ball at a constant speed when it hits peak speed, example, 15f. It is a Rigidbody2d collider ball.
As suggested in this post, you could control the velocity of the
your rigidbody using the Vector2.ClampMagnitude method while tracking it in your OnFixedUpdate(). I'm providing the code in the post I'm citing for your convenience. I edited the code I provided from the cited post to match the recent changes in the API:
float maxVelocity = 10;
void FixedUpdate()
{
rigidbody2D.velocity = Vector2.ClampMagnitude(rigidbody2D.velocity,
maxVelocity);
}

Categories

Resources