Skip to main content
17 votes

Moving player inside of moving spaceship?

The ship stays in place and everything moves. Mostly. In this answer I present three ideas of how to go about it, and I'm referencing the games I'm basing them on. I could not find out what approach ...
Theraot's user avatar
  • 27.2k
15 votes
Accepted

How can I "zero out" velocity in an arbitrary direction?

This is pretty simple linear algebra. You can make a little helper method for it: ...
DMGregory's user avatar
  • 135k
10 votes

How to move the player while also allowing forces to affect it

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 ...
Philipp's user avatar
  • 120k
9 votes

Why objects are falling equally in Unity

Check out the implementation of linearDamping in PhysX here (what Unity calls Linear Drag in the Rigidbody built on top of PhysX). It's equivalent to: ...
DMGregory's user avatar
  • 135k
7 votes
Accepted

Nestling into contact with a group of physics objects without exerting forces on them

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 ...
DMGregory's user avatar
  • 135k
7 votes
Accepted

How to move the player while also allowing forces to affect it

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 ...
gjh33's user avatar
  • 681
4 votes
Accepted

Why does applying force to a game object apply force to children, as well?

When we parent a game object to another game object, we make the Transform values of the child object local to the Transform ...
Gnemlock's user avatar
  • 5,273
4 votes
Accepted

How to disable gravity for a RigidBody moving on a slope?

I've had great success by the following method. The idea is that, under the specific circumstance that the Rigidbody is in contact with a surface appropriately angled in relation to gravity(which is ...
user2712865's user avatar
4 votes

How to move the player while also allowing forces to affect it

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 ...
DMGregory's user avatar
  • 135k
4 votes

How can I create a dice toss that results in a specific value?

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 ...
gjh33's user avatar
  • 681
4 votes
Accepted

Can a GameObject be both 2D and 3D?

No, it can't. Unity won't allow you to add both a Rigidbody and a Rigidbody2D to the same game object: It also won't allow you to add both a 3d Collider and a 2d Collider: What you can have, though,...
Philipp's user avatar
  • 120k
3 votes

implementing magnet like system in unity

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 ...
DMGregory's user avatar
  • 135k
3 votes

How to count rotations or spins when using addtorque?

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 ...
John Hamilton's user avatar
3 votes
Accepted

Unity : how to Rotate GameObject back to Indentity position?

I'd usually approach it like this: ...
DMGregory's user avatar
  • 135k
3 votes
Accepted

Objects do not stop when colliding each other while using mouse drag

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 ...
Tartle Wizard's user avatar
3 votes

Unity's CharacterController vs. Rigidbody with Collider

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) ...
Engineer's user avatar
  • 30k
3 votes
Accepted

OnTriggerEnter doesn't detect collision until at least one of the two colliders move

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 ...
Karv's user avatar
  • 51
3 votes
Accepted

Proper way to set a RigidBody's maximum velocity

What you want to do is not to set the max velocity, but void applying any additional force once you get to a certain speed. Something like the example below. Watch the if statement ...
Fabio S.'s user avatar
  • 465
3 votes
Accepted

How to check if NPCs are on ground or not?

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. ...
DMGregory's user avatar
  • 135k
3 votes
Accepted

Unity c#: How to slow down rotation over time?

As Tom mentioned, after your second if call, you might have a third if statement such as: ...
c4llmeco4ch's user avatar
3 votes
Accepted

Unity tilemap collider not colliding with rigidbody

I forgot to put a circle collider on the red circle. Leaving this up for others who might run into the same problem.
Siggytron's user avatar
3 votes

How can I create a dice toss that results in a specific value?

Revisiting this question, I just got a new idea: you could make a physics simulation in the background and cache the result, determine which face ended up, put the textures in the right place and play ...
Gyoo's user avatar
  • 286
3 votes
Accepted

How to set the force applied to RigidBody2D depending on the time user holds touch on screen?

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 ...
CosmicGiant's user avatar
  • 2,104
3 votes

Moving player inside of moving spaceship?

The main issues here are, roughly: Floating-point imprecision and catastrophic cancellation. An ordinary (single-precision) float has 23 bits of mantissa. Say you're moving at 1.0 m/s, and you add ...
TLW's user avatar
  • 161
2 votes
Accepted

Copy velocity of one object to other object

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: ...
jgallant's user avatar
  • 8,424
2 votes

Nestling into contact with a group of physics objects without exerting forces on them

If your problem here is that when player is placing the ball, it pushes other balls back, then why not just turn isKinematic on and off? Turn off physics for all the other balls on the playing field, ...
John Hamilton's user avatar
2 votes
Accepted

Obtaining vector representing local z axis orientation

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 ...
DMGregory's user avatar
  • 135k
2 votes
Accepted

How to count rotations or spins when using addtorque?

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 ...
Stormwind's user avatar
  • 1,063
2 votes
Accepted

Problem when trying to use rb.MoveRotation in Unity

Caution: The trick shown here of using input in FixedUpdate worked at the time it was written in 2017 — Unity at the time had fresh input available from the very beginning of the frame, contrary to ...
DMGregory's user avatar
  • 135k
2 votes

Unity, rb.MovePosition and transform.Rotate at the same time

Transform.Rotate is used for transforms (I know, excuse the redundancy). If you are already displacing with Rigidbody, then use Rigidbody.MoveRotation (it takes a quaternion as parameter). Example: <...
LifGwaethrakindo's user avatar

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