- Home /
A Problem with freeze rotation and position.
Hello. I am writing a script that freezes the position of objects and allows them to travel on a single axis when given the inputs of the assigned keys X, Y, and Z. This script starts with the variables x, y, and z all deactivated. This leads to my first problem. On start, my object is not constrained in two of the axis. Also, I intended to keep all axis of rotation constrained but none of them are constrained.
Could it be that I am using a modified version of the dragrigidbody script to move the object? I changed lines 20 and 63 on the dragrigidbody script from:
mainCamera.ScreenPointToRay (Input.mousePosition)
to:
mainCamera.ViewportPointToRay(new Vector3(0.5, 0.5, 0))
Here is my drag object script:
#pragma strict
var x : boolean = false;
var y : boolean = false;
var z : boolean = false;
function Start () {
x = false;
y = false;
z = false;
}
function Update () {
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
//If it is false
if(x == false){
rigidbody.constraints = RigidbodyConstraints.FreezePositionX;
}
if(y == false){
rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
}
if(z == false){
rigidbody.constraints = RigidbodyConstraints.FreezePositionZ;
}
//If it is true
if(x == true){
rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
}
if(y == true){
rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ;
}
if(z == true){
rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionX;
}
if(Input.GetKeyDown(KeyCode.X)){
x = true;
y = false;
z = false;
}
if(Input.GetKeyDown(KeyCode.Y)){
x = false;
y = true;
z = false;
}
if(Input.GetKeyDown(KeyCode.Z)){
y = false;
x = false;
z = true;
}
}
Are there any bugs that I can fix? For the rotation constraints I already tried "rigidbody.constraints = RigidbodyConstraints.FreezeRotation;" with no luck.
Here is my updated script with the rotation problem fixed:
#pragma strict
var x : boolean = false;
var y : boolean = false;
var z : boolean = false;
function Start () {
rigidbody.constraints = RigidbodyConstraints.FreezePosition;
}
function Update () {
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
if(Input.GetKeyDown(KeyCode.X)){
x = true;
y = false;
z = false;
}
if(Input.GetKeyDown(KeyCode.Y)){
x = false;
y = true;
z = false;
}
if(Input.GetKeyDown(KeyCode.Z)){
y = false;
x = false;
z = true;
}
//If it is false
if(x == false){
rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
}
if(y == false){
rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
}
if(z == false){
rigidbody.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
}
//If it is true
if(x == true){
rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
}
if(y == true){
rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
}
if(z == true){
rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
}
}
Answer by DarkGamer67 · Jul 21, 2014 at 05:31 PM
I am not sure. I think need to set the RigidbodyConstrains to none because the Rotatons stay freezed if they won't get reset. You need to reset them because you freeze all Layers every Frame and you don't need to do something if it's false. Your script should look like this :
#pragma strict
var x : boolean = false;
var y : boolean = false;
var z : boolean = false;
function Start () {
x = false;
y = false;
z = false;
}
function Update () {
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
//If it is true
if(x == true){
//First reset the feezed layers
rigidbody.constraints = RigidbodyConstraints.None;
//Than freeze the Layers
rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
}
if(y == true){
//First reset the feezed layers
rigidbody.constraints = RigidbodyConstraints.None;
//Than freeze the Layers
rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ;
}
if(z == true){
//First reset the feezed layers
rigidbody.constraints = RigidbodyConstraints.None;
//Than freeze the Layers
rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionX;
}
if(Input.GetKeyDown(KeyCode.X)){
x = true;
y = false;
z = false;
}
if(Input.GetKeyDown(KeyCode.Y)){
x = false;
y = true;
z = false;
}
if(Input.GetKeyDown(KeyCode.Z)){
y = false;
x = false;
z = true;
}
}
Thank you DarkGamer67! I have actually fixed the rotation problem (I needed to put "freeze rotation" everywhere and now the script is a little messy). The only problem that remains is the fact that the object starts constrained in position in only one axis and not all three. Thank you for your version of the script! When I play your version, the object starts out movable in all axis and when I press X, Y, or Z, it is not constrained in the rotation.
Thank you again and I will post the updated script!
Your answer

Follow this Question
Related Questions
DragRigidbody 1 Answer
Spin non kinematic object ? 1 Answer
Freezing Y position when using rootmotion 1 Answer
Dragging object inside TriggerZone 0 Answers