Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Weither · Sep 30, 2013 at 04:32 AM · rotationrigidbodytransformaxis

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?

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Sep 30, 2013 at 04:49 AM 0
Share

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.

avatar image Weither · Sep 30, 2013 at 09:24 AM 0
Share
     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?

avatar image robertbu · Sep 30, 2013 at 09:54 AM 0
Share

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.

avatar image Weither · Sep 30, 2013 at 12:15 PM 0
Share

do you know any command which will freeze X value in rotation?

avatar image robertbu · Sep 30, 2013 at 04:37 PM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges