I have an object in unity with a rigidbody attached to it. For some reason, the object moves forward on the Z axis, even though no script is telling it to.
Does anyone know why this is happened?
(some notes, isKinematic is turned off, freeze x, y position are on, free x, y, z position are on)
An Object does not need to have a script directly attached to it for it to move. A script that is attached to another GameObject can also find this GameObject and be able to move it.
You should disable all other GameObjects with scripts. If they contain any script that is moving this GameObject, you will find out.
Another big possibly I've seen several times is the Animation or Animator component. These two components can move a GameObject and I suggest you remove any of them that is attached to your GameObject. It is very likely this is the problem.
An impulse could have affected it, and the velocity is still up.
Do you have windzones in your scene?
Try to disable the ridgidbody during play, if any script is acting on your Ridgidbody, you will get an error pointing to the class trying to use it.
Disable the collider, see if it still moves.
If colliders are too small, ridgidbodies tend to "dance"
SOLUTION: In inspector freeze rotation X and Z in the Сonstraint of Rigidbody.
Perhaps this is a bug of the physical system. In my case, the capsule began to spontaneously go up the slope, even without any scripts and animation. Unity version 2021
Related
I am trying to play the animation but whenever i add rigidbody, player starts to float in the air at the constant value (which is 3.5 Y). No script is attachted atm.
I tried to remove rigidbody which solved the problem, but i need it so deleting rb is not an option. Gravity values are normal and the other animations are from mixamo (i didn't do the animations).
Based on the information provided here is what I'm thinking:
The root cause of this is that your character model when effected by gravity is being pushed to the ground and foot IK is causing the legs to end up looking like your screenshot above.
Some common approaches to this problem:
Create an empty object called player (Controls Rigidbody/Capsule Collider/Character Logic)
Add the 3d Model as a child of this object (Animator/Mesh/etc).
On the root object add your rigidbody as well as a capsule collider (prevents character model from colliding with the environment due to gravity).
With this setup animations can be done on the model (+foot IK) without gravity causing issues.
Okay i just solved the problem. Problem was neither rigidbody nor animation.
The collider of mesh (ground) has option checked "is Convex". I turned it off, problem solved, player is not floating anymore.
I am a newbie in Unity. I have searched on the internet for tutorials on how to pick up GameObjects and followed them as it is. But here's the problem:
This here is when I set the Camera as a parent to GameObject, initially....
And here's what happens when I move the camera.
The problem here is that the Object doesn't stay on the crosshair
This happens only when moving the camera along the Up-and-Down Axis (not sure if x or y).
However, this doesn't seem to be a problem in any tutorials. Is there any part of the code missing?
P.S:- I have set the Camera as a parent to the GameObject in the hierarchy directly without any code.
It is difficult to know the exact problem without at least a video of it, but the first thing here is to make sure there's nothing messing with the object's transform outside of camera's parenting.
I can't seem to wrap my head around the following, I have a simple spaceship with 2 turrets:
My 'Player' the white ship has a rigidbody. The turrets have not.
I'm moving and rotating my player via the rigidBody, however I would like to also rotate the turrets via the physics system.
I thought about adding a rigidBody2D to the turrets, however that was not recommended from my understanding reading posts online. This is because rigidbodies in children would affect the parent in unforseen ways.
So dropping that idea, How can I move my turret and having it be affected by physics?
Some sidenotes:
I want the turrets to be influenced by physics. Lets say i try to rotate my turret, but it hits a wall, I want it to resist. Lets say some debris hit my turret's barrel, I want it to move in the opposite direction.
Although i could move the turret outside of its parent's container (the ship) I do ofcourse want it to 'stay on the ship in game'. The turret is part of the ship, its mounted to the hull or fuselage if you will. So it should move with the ship and keep its position.
As mentioned by derHugo joints where up for the task, although i had to restructure a bit, here is the outcome:
I added an empty transform to hold the ship and its sub-components, so turrets are no longer part of the ship parent, so that i can add a rigidBody to these child objects now.
Then I added a hingejoint and rigidbody to the player ét voila, it now works:
I still need to take a good look at how joints work exactly but this seems to work.
I am trying to make some kind of simulation program with Unity. The simulation includes a missile launched from an aircraft and a terrain.
I get the coordinate data required for the movement of the missile from another program using a socket connection.
I created an explosion effect for the missile to explode as soon as the missile and the terrain collided. But the explosion effect is not triggered in any way.
I used the OnCollisionEnter() method to detect the collision, but this method does not seem to work.
The missile has its own rigidbody and collider and The terrain has Terrain Collider, but still no collision is detected and the missile passes through the terrain.
What could be the cause of this error?
EDIT :
I thank everyone for their help, but none of the solutions worked. I solved the error using the OnTriggerEnter method. For this, I also had to enlarge the object's collider a little more.
As mentioned in the comment section above (I dont have enough rep to contribute to the discussion but will provide a solution), you need to enable continuous collision detection. Once you have done that we can move onto the next step.
It is generally bad practice to interact with rigidbodies in update and it can lead to all sorts of strange bugs so I wouldn't recommend it. That being said I dont think it's the cause of your issues. As #HumanWrites mentioned, you are manually moving the position each frame which causes your missile to never actually collide with your mesh. the solution to this is:
rb.MovePosition(Vector3.MoveTowards(...))
The reason for this is that by using the method on the rigidbody, you inform the physics engine that you want to move from one position to the other and you would like the physics to take care of this instead of you doing it directly. This allows it to "sweep" between the current position and target position and detect any collisions that would have happened along the way.
Your missile is moving too fast to ever actually touch the mesh within a frame so you need to rely on the physics engine doing that sweep check.
Context
I have in my project two elements. A player (just a cube) and some eyewears.
When the glasses aren't attached to the player, i want them to have properties of a Rigidbody. But when the eyewears are attached to the player, i want them to be a static object, so the player can process the collisions and physics.
I have tried:
DestroyElement(rigidbody) when the player pickup the eyewears. When he leaves them, i recreate the rigidbody with AddComponent
It worked nice, but in the future other elements will be attached and they will not share the same properties of rigidbody. I though maybe i could save the rigidbody instance, so when the player leaves the glasses i assign it to them. I could't.
AddComponent don't accept arguments.
Then i tried to set "kinematic mode" when my player wears the eyewears. It didn't go well, my player can't jump anymore and sometimes he glitches in the floor.
How can i resolve this?
GameObject.AddComponent does take an argument, or a generic argument (preferred):
go.AddComponent<RigidBody>();
this is also possible, but deprecated, since you lose type-safety:
go.AddComponent(typeof(RigidBody));
However, RigidBody is not meant to be added/removed, and in your case I would say that kinematic mode is the way to go... but I can't tell why you're experiencing weird results with it.
Thanks to R Astra i checked again the eyewear collisions and i found out the problem. I had enabled the Convex mesh.
The col was causing trouble because the back meshes were inside the player
So, quickly i copied that eyewear and generate a new mesh. It worked!
Thank you very much!