- Home /
Copy rotation of another object with weight variable
I am trying to make my own version of this component with a script. Basically I was going to use this component, but then I realized that it didn't include a feature that I was hoping it had. So can someone help me write the script that acts as this component? I'm mainly confused on how to make the "Weight" variable so that it affects how much the object copies the rotation of another object.
Answer by toddisarockstar · Nov 11, 2018 at 03:12 AM
im not sure what the weight variable is sopposed to do but this would freeze the rotation of an object on seperate axises to however it was placed in the scene if you make the bools true
public bool freezeX;
public bool freezeY;
public bool freezeZ;
Vector3 startrot;
void Start () {
startrot = transform.eulerAngles;
}
void Update () {
float curx = transform.eulerAngles.x;
float cury = transform.eulerAngles.y;
float curz = transform.eulerAngles.z;
if(freezeX){curx = startrot.x;}
if(freezeY){curx = startrot.y;}
if(freezeZ){curz = startrot.z;}
transform.eulerAngles = new Vector3 (curx, cury, curz);
}
Thanks. That will definitely help me get started! And the Weight variable is a float value from 0 to 1 (so basically a percentage). Based off of that percentage, it will copy a desired object's rotation by that much percent. So like if the weight was set to 0.5f and the object rotated 180 degrees, it would rotate the current object to 90 degrees. If it was set to 1 however, the current object would be set to 180 degrees just like the other object (because it copies 100% of the other object's rotation).
Do you know how I could implement this in the code?
Answer by A_Lego · Nov 11, 2018 at 03:18 AM
You can use Quaternion.Lerp(this.rotation, otherObjects.rotation, percent))
in conjunction with some creativity to create your desired effect!
(PS, percent has to be between 0f and 1f)