- Home /
How to lock X rotation; broken script
Hi. Here's my code:
void FixedUpdate()
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if (Vector3.Distance (transform.position,target.transform.position) < 30 || immaAtacked == true)
{
animation.Stop("stay");
immaAtacked = true;
animation.Play ("walk");
myRigidbody.AddForce(myTransform.forward * movementSpeed);
}
}
I tried something like that:
void FixedUpdate()
{
var lol = Quaternion.LookRotation(Vector3(myTransform.position.x, target.position.y, target.position.z)-myTransform.position);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(lol), rotationSpeed * Time.deltaTime);
if (Vector3.Distance (transform.position,target.transform.position) < 30 || immaAtacked == true)
{
animation.Stop("stay");
immaAtacked = true;
animation.Play ("walk");
myRigidbody.AddForce(myTransform.forward * movementSpeed);
}
}
But i've got errors here. Anybody know what can I do with this?
When posting questions like this one, please include the error message for the first couple of errors, and including the entire script is very helpful.
I don't know about your logic, but I see two syntax errors. On line #3, you need a 'new' keyword in front of your Vector3 call. On line #4 you use 'Quaternion.LookRotation(lol)', but lol is a Quaternion, not a Vector3. There is no version of LookRotation() that takes a Quaternion as a parameter. $$anonymous$$y guess is you want just 'lol' here.
While not a bug, I personally would use 'Quaternion' ins$$anonymous$$d of 'var' on line 3.
void FixedUpdate()
{
Quaternion lol = Quaternion.LookRotation(new Vector3(myTransform.position.x, target.position.y, target.position.z)-myTransform.position);
53> myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(lol), rotationSpeed * Time.deltaTime);
if (Vector3.Distance(transform.position,target.transform.position) < 30 || immaAtacked == true)
{
animation.Stop("stay");
immaAtacked = true;
animation.Play ("walk");
myRigidbody.AddForce(myTransform.forward * movementSpeed);
}
error CS1503: Argument #1' cannot convert
UnityEngine.Quaternion' expression to type UnityEngine.Vector3' error CS1502: The best overloaded method match for
UnityEngine.Quaternion.LookRotation(UnityEngine.Vector3)' has some invalid error CS1502: The best overloaded method match for UnityEngine.Quaternion.Slerp(UnityEngine.Quaternion, UnityEngine.Quaternion, float)' has some invalid arguments error CS1503: Argument
#2' cannot convert object' expression to type
UnityEngine.Quaternion'
All in 53. What can I do?
As mentioned in my previous comment, you cannot pass a Quaternion to a LookRotation. As mentioned Quaternion.LookRotation(lol) is not valid. Again, I think you want just 'lol'. That is Slerp() takes Quaternions, and lol is a Quaternion. Note this just addresses compiler errors. I did not comb your code to see if it would do what you want.
do you know any command which will freeze X value in rotation?
Conceptually what you are doing is what I usually do...move the target point. One issue you may be having is that you are not specifying an 'upwards' in the LookRotation(). Try this:
Quaternion lol = Quaternion.LookRotation(new Vector3(myTransform.position.x, target.position.y, target.position.z)-myTransform.position, Vector3.right);
Your answer
Follow this Question
Related Questions
Problem changing rotation of 3rd Person Controller 1 Answer
How to find the angle of a rotation? 1 Answer
Can I limit the rotation of an Object on an axis between a Negative and Positive value? 2 Answers
Rotating An Object On Its Axis Once 3 Answers
Player character X rotation going haywire on game start 1 Answer