- Home /
using Aim Constraint
hey everyone, i want to use aim constraint but i want to control disable and enable freeze rotation axes by script, how i would do it ??
thanks,,
It'd be something like
aim.rotationAxis = Axis.Y;
Not counting as an answer as that might be reversed (I didn't know this existed until 10 seconds ago).
Answer by lgarczyn · Oct 17, 2019 at 10:50 PM
For some reason, this setting appears absent from the API. But you can create a basic version of this script like so:
public Transform target;
Quaternion initRotation;
void Start() {
initRotation = transform.rotation;
}
void Update()
{
Vector3 targetDir = target.position - transform.position;
Quaternion lookRotation;
if (locked)
lookRotation = Quaternion.LookRotation(Vector3.up, targetDir)
* Quaternion.FromToRotation(Vector3.up, Vector3.forward);;
else
lookRotation = Quaternion.LookRotation(targetDir, Vector3.up);
transform.rotation = initRotation * lookRotation;
}
Untested, but should work. Do tell me if you have any issues.
Edit: you can actually access them like this
using UnityEngine.Animations;
[...]
GetComponent<AimContraint>().rotationAxis = Axis.X | Axis.Z; // to tick every box except Y
or like this
GetComponent<AimContraint>().rotationAxis = Axis.None; // to unlock the rotation
The "Freeze Rotation Axis" label is very confusing, considering that activating them is what allows the AimConstraint component to change them. "Constrained Axis" would make a lot more sense.
Your answer
Follow this Question
Related Questions
Same script working on one object at a time. 1 Answer
Can't determine how to disable certain objects. 0 Answers
How do I enable and disable multiple game objects in Unity? 1 Answer
error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer 3 Answers
How do I disable all rigidbodies and colliders in a scene in unity? 2 Answers