- Home /
Rigidboday fixed rotation not working.
I've got a player GameObject
thats a Rigidbody
. This has been setup to Freeze Rotation on X and Z.
If I start bumping into walls the Rigidbody
still rotates...
I event tried to force the issue in script:
void FixedUpdate () {
if (!isServer) {
return;
}
Quaternion startRotation = _rigidbody.rotation;
if (_moveForward) {
float moveAmount = speed * Time.deltaTime;
_rigidbody.MovePosition(_rigidbody.position + _rigidbody.transform.forward * moveAmount);
}
if (_moveBackward) {
float moveAmount = (-speed * 0.6f) * Time.deltaTime;
_rigidbody.MovePosition(_rigidbody.position + _rigidbody.transform.forward * moveAmount);
}
if (_turnLeft) {
Quaternion rotateAmount = Quaternion.Euler(new Vector3(0f, -angularSpeed, 0f) * Time.deltaTime);
_rigidbody.MoveRotation(_rigidbody.rotation * rotateAmount);
}
if (_turnRight) {
Quaternion rotateAmount = Quaternion.Euler(new Vector3(0f, angularSpeed, 0f) * Time.deltaTime);
_rigidbody.MoveRotation(_rigidbody.rotation * rotateAmount);
}
if (_jump && _isGrounded) {
_isJumping = true;
}
if (_isJumping && jumpTimeLeft > 0) {
float moveAmount = jumpSpeed * Time.deltaTime;
_rigidbody.MovePosition(_rigidbody.position + _rigidbody.transform.up * moveAmount);
jumpTimeLeft -= Time.deltaTime;
} else if (_isJumping) {
_isJumping = false;
jumpTimeLeft = jumpTime;
}
if (_shoot) {
_isShooting = true;
}
if (_isShooting && shootTimeLeft == shootTime) {
GameObject go = ObjectPool.instance.GetObject ("Normal Bullet");
go.GetComponent<NormalBullet> ().owner = _rigidbody.gameObject;
shootTimeLeft -= Time.deltaTime;
} else if (_isShooting && shootTimeLeft > 0) {
shootTimeLeft -= Time.deltaTime;
} else if (_isShooting && shootTimeLeft <= 0) {
_isShooting = false;
shootTimeLeft = shootTime;
}
byte bits = (byte)_rigidbody.constraints;
Quaternion frozenRotation= new Quaternion(
(bits & (1 << 4)) != 0 ? startRotation.x : _rigidbody.rotation.x,
(bits & (1 << 5)) != 0 ? startRotation.y : _rigidbody.rotation.y,
(bits & (1 << 6)) != 0 ? startRotation.z : _rigidbody.rotation.z,
_rigidbody.rotation.w
);
_rigidbody.rotation = frozenRotation;
}
How to I freeze the rotation of a GameObject
with a Rigidbody
on it?
Answer by 5c4r3cr0w · Mar 14, 2016 at 02:05 PM
Well, When you control a rigid body movement with transform, freezing rotation wouldn't help because it simply overwrites physics engine values. Use _rigidbody.velocity instead of _rigidbody.MovePosition and use _rigidbody.angularVelocity instead of _rigidbody.MoveRotation. This will work because it allows the physics engine to controll the constrains you have given to rigidbody.
Hope it helps. :)
Hi,
$$anonymous$$y understanding is I was using the Physics engine to move because I was using $$anonymous$$ovePosition and $$anonymous$$oveRotation. As far as I know I'm not accessing the GameObject.transform or the Rigidbody.transform directly.
I would like the ship to interact with the physics system but do not want it controlled by the physics system with forces. For example I only want it moving when the keys are pressed.
" I would like the ship to interact with the physics system but do not want it controlled by the physics system with forces." - It's perfect example of case, where You want check "Is $$anonymous$$inematic".
$$anonymous$$inematic Rigidbody still can push, move and such on other objects, but other objects can't do the same for $$anonymous$$inematic Rigidbody.
Your answer
Follow this Question
Related Questions
Rigidbody Freezerotation - Freezing one angle? 2 Answers
How do i keep a rigidbody upright? Looking for best way to freeze rotation 1 Answer
How to make the character move and rotate where the camera is facing (3rd person) 0 Answers
Rigidbody Rotation Problem 0 Answers
Rigidbody +parenting +rotation +elevator = Player unwanted behavior(pushed away) 1 Answer