9

I created a little demo game a few days ago which demonstrates different ways to move a player-character. It might help you to better understand which way of moving is the right one for your particular game. In general, you should use rigidbody.AddForce whenever feasible. It automatically takes care of managing multiple overlapping forces and ensures that ...


5

You're on the right track! This is something that usually the controller is responsible for. When you jump in a platformer you use a "isGrounded" variable to change how the controls behave while in the air right? You need a similar state for isKnockback. In most games when a player is knocked back, they have control striped from them for a certain amount of ...


4

This might be the correct expected behaviour for the code you've written. Let's run through some math. I'll assume for the moment that you're using Unity's default FixedUpdate rate of 50 Hz and that your character has a mass of 1. If I press right on my stick (on the ground), my character gets an x velocity of 5 m/s. If I do this in the air, then since ...


4

When we parent a game object to another game object, we make the Transform values of the child object local to the Transform values of the parent object. This is very useful, if used correctly. In the above situation, we have a character holding a ball. While we move the character with the assumption of still holding on to the ball, we can simply move the ...


4

I think what you're looking for is "damping." This is where you drain a little energy out of the system with each update step so that it settles into an equilibrium, rather than oscillating/vibrating forever. You can do this by multiplying the velocity each frame by a value close to but less than one (the lower the value, the more sharply your object ...


4

Myself, I like to solve this by thinking of all player movement as acceleration-based. I choose a target velocity using whatever complicated control logic I like, then ask the player avatar to accelerate toward that target, while respecting maximum acceleration rates I set. Then, depending on the avatar's state (on ground, on ice, in the air, in a ...


4

I wouldn't rely on the physics for this. If you have one dice, then you should just have some prebaked animations. You can have more than one animation per outcome to add variation. If you have multiple dice then we can take a different approach. Prebake several animations (you can use physics and record the keyframes) for rolling the dice. For each dice, ...


3

Each rigid body has an attribute indicating if it's affected by gravity or not. You can just mark that attribute as true, whenever player enters the spaceship, and later turn it false, when he exits. But that'll limit you with a big limitation. The gravity itself in unity is something global, meaning all entities are affected by same gravity force (if any). ...


3

So far i have figured out, that it might has to do with the center of mass and the moment of inertia. You're right, it does have to do with the center of mass and the inertia. The resultant force is simply, the original force plus the force applied. The resultant torque is equal to the original torque plus the cross product of the force vector with the ...


3

I will post this answer since this question was resolved by me in the comments above. In order to get the velocity of the VR controller, you need to use: var device = SteamVR_Controller.Input((int)trackedObj.index); device.velocity;


3

The drag movement is not being applied through a force on the rigidBody but directly on the object's transform position, so what you are trying to do wont work. Instead you need to detect the collision and then disable OnMouseDrag with a boolean value sort of like this: bool allowDrag = true; void OnMouseDrag() { if(allowDrag) { //... do ...


3

Here's a way to do this using Physics.SphereCast to test against the bodies' colliders without actually exerting any forces on them. It assumes that your cursor object is placed on exactly the same y plane as the centers of the balls, so that all the collisions are in-plane against their widest circumference, and the camera is looking straight down at the ...


3

I'd usually approach it like this: // Tracks whether we have a movement in progress. Coroutine _currentMovement; // Interrupts the current movement, if any, and starts a new one. void StartMovement(IEnmumerator movement) { if(_currentMovement != null) StopCoroutine(_currentMovement); _currentMovement = StartCoroutine(movement); } // I ...


3

Get the difference between the last and the current EulerAngles variable, add it to another variable, check that variable and if the variable is over 360, subtract 360 from it and add one to the spin counter. So, something like this (might not work exactly as it's typed in, didn't test the code but it can at least be used as pseudo-code): float rollDegree =...


3

It's not either-or. It's both. Stick with the CharacterController initially. Go through the initial pain of writing it the way you want it under normal (kinematic) control circumstances, as there are things that Rigidbodys do to player characters that you won't like under various circumstances. It's like RL - we're generally in fine motor control of our ...


3

OK, after a bit of tinkering, my brother and I finally found the answer. The issue is as follows: Enabling/disabling a BoxCollider (or any collider for that matter) doesn't wake up the RigidBody to which it's attached, but movement does. That's why it only worked when moving. All we needed is to make the Game Object's RigidBody2D to "Never Sleep". It's as ...


3

My advice for pedestrian-type NPCs would be: don't. For player characters, we often want to give them full freedom of motion to run and climb and leap or launch themselves around all over the place. That usually involves some form of physics simulation, and checking to see whether they've landed/caught a ledge/gobs of other complex motion states and contact ...


3

As you can see I able to get the Time.deltaTime which is I assume is how long the player hold the screen. Your assumption is wrong. Time.deltaTime gives you the time elapsed between the last frame and current frame. Your question boils down to "How do I measure touch time across multiple frames?". And the answer is simple: You accumulate the delta time ...


2

This: if (coll.gameObject.tag == "Ball") gameObject.SetActive(false); Application.LoadLevel(Level); You're missing parenthesis there. Application.LoadLevel isn't affected by the if condition, so it's firing every time. Turn it to this: if (coll.gameObject.tag == "Ball") { gameObject.SetActive(false); ...


2

Here is a simplified example of how you could implement a check to see if your gameObject is grounded or not. It is not perfect, but should probably work for your situation: bool Grounded; void OnCollisionStay2D(Collision2D collider) { CheckIfGrounded (); } void OnCollisionExit2D(Collision2D collider) { Grounded = false; } private void ...


2

This code in my template project runs perfect and trow object diagonally: if (Input.GetKeyDown(KeyCode.Space)) { rigi.velocity = Vector3.zero; rigi.AddForce(new Vector3(30, 30, 0), ForceMode.Impulse); } So, i assume some possible reasons: You have wrong Vector3 newVel initialization.. Your if statement calls multiple times and ...


2

Assuming that rightPaddle and pongBall are both game objects, your code would look something like the following: Vector3 paddleOldPos = rightPaddle.transform.position; Vector3 ballPos = pongBall.transform.position Vector3 newPos = new Vector3(paddleOldPos.x, ballPos.y, paddleOldPos.z); rightPaddle.transform.position = newPos; You can't modify a transform'...


2

This code is from my own physics engine (C#). It's 2D, but it's just the same in 3D. The forces are simply accumulated, and the rotation (torque) produced is the cross product of the vector from the center of the object to the point and the force applied. public void ApplyForceAtPoint(Vec2 f, Vec2 point) { force += f; torque += Vec2.Cross(point - ...


2

void OnCollisionEnter2D(Collision2D target) { if (target.gameObject.tag == "Player") { Vector2 normal = target.contacts[0].normal; if (normal.y < 0) { gameObject.GetComponent<Rigidbody2D>().isKinematic = false; } else { //Make player pass trough platform if you want this ...


2

Take into account that Applying a force is not the same as changing the velocity of the object. Without friction, the result would be very similar, but when an object is standing in a surface, and there is friction between them, the behaviour could be very different. In the videos you uploaded, the cube is rolling because you are pushing it from a side, ...


2

There is just a minor mistake in this code which is transform.forward, which points to the z-axis which is obviously not working in a 2D game. So instead of this : _bullet.GetComponent<Rigidbody2D>().AddForce(origin.transform.forward * thrust); use this and it should work just fine : _bullet.GetComponent<Rigidbody2D>().AddForce(origin....


2

I would simply use GetComponent in your foreach as you're doing and assign a variable, like so: Collider[] colliders = Physics.OverlapSphere(hit.point, radius); foreach (Collider c in colliders) { Rigidbody r = c.GetComponent<Rigidbody>() if (r == null) continue; r.AddExplosionForce(force, hit.point, radius, 1, ForceMode.Impulse); } ...


2

Might be advantageous to not demand a totally full circle (= airplane back completely horizontal rotation) for an "accepted roll" - maybe it's sufficient, and more user-friendly, to reach an almost horizontal position in order to consider it a completed roll? In addition, it's always a bit hairy to use an accumulation over a long period of time - it may ...


2

transform.forward already gives you exactly this. It looks like you might just be confused about how to visualize the vectors. You're repeatedly visualizing the vector (0, 0, 10) with different start points, not visualizing the facing direction of transform.forward. Try this: // Start our drawn line at the object's position in world space. Vector3 ...


2

I'm assuming you're referring to the player being detected as touching the ground, and not every object? Regardless; simply place a collider on your floor. Then add the following script to your floor. C# OnCollisionEnter(Collision col){ if(col.gameObject.tag == "Player"){ col.getComponent<myCharacterScript>().isOnFloor = true; } OnCollisionExit(...


Only top voted, non community-wiki answers of a minimum length are eligible