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;
}
}