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
1
Question by Ozzyel-PT · Jul 27, 2013 at 05:41 AM · javascriptrotatebones

Rotating a bone in Z axis.

Hello Everyone. I'm pretty new at unity and new on this Community. I just want to know how to rotate a bone of a 3rd Person animated character, I want the char to look up and down by moving the mouse up and down. I have this script so far, and works perfect with X and Y axes but on Z:

 var spine : Transform;    
 var spineControl = 0.9;
 private var currentDirection = Vector3.forward;

 function LateUpdate ()    {
    
        currentDirection.z = Mathf.Clamp(currentDirection.z + Input.GetAxis("Mouse Y"), -2, 2);
        var rotation : Quaternion = Quaternion.LookRotation(currentDirection);
        spine.localRotation = Quaternion.Lerp(spine.localRotation, rotation, spineControl);
 
   
     }

Thanks for your help.

//A preview of my first work on unity: http://www.youtube.com/watch?v=X1I3Nq00Uho

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by daivd.ramz · Jul 27, 2013 at 08:51 AM

hi, i use this script for rotate a bone of my 3rd character and work nice i hope this work nice for you:

      var bone:GameObject;
 
      private var z:float=0;
      private var ySpeed:float=10;
      private var zMinLimit:float= -40.0;
      private var zMaxLimit:float= 20.0; 
 
      function Update()
      {
      
           z -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
      
           z = ClampAngle(z, zMinLimit, zMaxLimit);
     
           Quaternion rotation_bone= Quaternion.Euler(0, 0, z);
     
           bone.transform.localRotation = rotation_bone;
      }
 
      static function ClampAngle (float angle, float min, float max) 
      {
       if (angle < -360)
             angle += 360;
       if (angle > 360)
             angle -= 360;
       return Mathf.Clamp (angle, min, max);
      } 

 
  
Comment
Add comment · Show 4 · Share
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 Ozzyel-PT · Jul 27, 2013 at 07:09 PM 1
Share

Thanks man, it was very helpful, I just made some modification and it's solved now. There is the code working for me:

 var spine:GameObject;
  private var z:float=0;
  private var ySpeed:float=13.0;
  private var z$$anonymous$$inLimit:float= -40.0;
  private var z$$anonymous$$axLimit:float= 40.0; 
  
  function LateUpdate() {
     z -= Input.GetAxis("$$anonymous$$ouse Y") * ySpeed;
     z = ClampAngle(z, z$$anonymous$$inLimit, z$$anonymous$$axLimit);
     var rotation_bone : Quaternion = Quaternion.Euler(0, 0, -z);
     spine.transform.localRotation = rotation_bone;
  }
 
 static function ClampAngle (angle : float, $$anonymous$$ : float, max : float) {
     if (angle < -360){
         angle += 360;
         }
     if (angle > 360){
         angle -= 360;
         }
     return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max);
  } 

   
avatar image daivd.ramz · Jul 28, 2013 at 06:14 AM 0
Share

Ok man, i wish good luck for you :)

avatar image JJDeveloper · Jul 02, 2015 at 07:53 PM 0
Share

Very cool. I tried to convert your code into C#. Can anyone help fix the bugs? I did most of it.

     GameObject spine;
 private float z = 0;
 private float ySpeed = 13.0f;
 private float z$$anonymous$$inLimit = -40.00f;
 private float z$$anonymous$$axLimit = 40.00f;

 void LateUpdate() {
     z -= Input.GetAxis("$$anonymous$$ouse Y") * ySpeed;
     z = ClampAngle(z, z$$anonymous$$inLimit, z$$anonymous$$axLimit);
     Quaternion rotation_bone = Quaternion.Euler(0, 0, -z);
     spine.transform.localRotation = rotation_bone;
 }
 
 static void ClampAngle (float angle, float $$anonymous$$, float max) {
     if (angle < -360){
         angle += 360;
     }
     if (angle > 360){
         angle -= 360;
     }
     return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max);
 } 
avatar image Ozzyel-PT · Jul 02, 2015 at 09:58 PM 0
Share

In line 14 use:

 static float ClampAngle (float angle, float $$anonymous$$, float max)

avatar image
0

Answer by glaxowinso · Sep 22, 2018 at 10:33 AM

Hi, i know this is old but just in case people still look into this, im new in unity and is making a 3rd person character i made a similar code before but it had the same results as this code. my character seems to be tilting side ways instead of up and down is there anyway to fix this? im really desperate :( alt text


ewew.jpg (70.2 kB)
Comment
Add comment · Show 1 · Share
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 glaxowinso · Sep 23, 2018 at 06:25 AM 0
Share

i figured it out and added a line in which i would rotate also i tried it with this script i changed the

Quaternion.Euler(0, 0, -z); to Quaternion.Euler(z, 0, 0);

i really should have added the same line before.

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

17 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 avatar image avatar image avatar image

Related Questions

change the rotation 1 Answer

A node in a childnode? 1 Answer

changing a coroutine's argument at the coroutine's runtime? 1 Answer

Moving an object 0 Answers

Debug states its true but if doesnt trigger. 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