- Home /
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
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);
}
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);
}
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);
}
In line 14 use:
static float ClampAngle (float angle, float $$anonymous$$, float max)
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 :(
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
![](https://koobas.hobune.stream/wayback/20220613110954im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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