- Home /
How can i rotate the flashlight to the other side by 180 degrees ?
I tried to rotate it on the x then on the z but it's always facing up. The original position: The flashlight is facing back to the left.
When i check the checkbox in the Inspector on the Ik Active in the IK Control script i want the flashlight to rotate to the other side and to be facing to the right.
But this is what i'm getting when checking the checkbox on Ik Active: The flashlight is facing up:
The script:
using UnityEngine;
using System;
using System.Collections;
[RequireComponent(typeof(Animator))]
public class IKControl : MonoBehaviour
{
protected Animator animator;
public bool ikActive = false;
public Transform rightHandObj = null;
public Transform lookObj = null;
void Start()
{
animator = GetComponent<Animator>();
}
//a callback for calculating IK
void OnAnimatorIK()
{
if (animator)
{
//if the IK is active, set the position and rotation directly to the goal.
if (ikActive)
{
// Set the look target position, if one has been assigned
if (lookObj != null)
{
animator.SetLookAtWeight(1);
animator.SetLookAtPosition(lookObj.position);
}
// Set the right hand target position and rotation, if one has been assigned
if (rightHandObj != null)
{
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position);
//animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);
lookObj.rotation = Quaternion.Euler(180, 0, 180);
}
}
//if the IK is not active, set the position and rotation of the hand and head back to the original position
else
{
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 0);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 0);
animator.SetLookAtWeight(0);
}
}
}
}
I changed this line:
animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);
With the line:
lookObj.rotation = Quaternion.Euler(180, 0, 180);
Since i want to rotate the flashlight when it's active and to rotate the hand. But i tried: 180,0 ,0 ,0 then 0, 0, 180 and now 180, 0, 180 but the flashlight is pointing up not to the right.
( And when it's not active is it logic the flashlight to be facing to the left like in the original screenshot ? I mean like in idle when not using the flashlight or i should change the flashlight position/rotation to something more logic then facing left ? )
Answer by IvovdMarel · Apr 27, 2017 at 06:37 PM
Keeping it simple: How about you create an Empty GameObject, parent the flashlight to it, and rotate it locally?
This could work, but just be aware: you could use JavaScript to do this. It's a lot simpler. Plus, it doesn't block direct modifications, such as:
Rigidbody rbody.velocity.x = 1;
At C# it looks like this:
Rigidbody rbody.velocity = new Vector3(1, 0, 0);
Try to do something like:
if (ikActive)
{
transform.rotation.x = 180;
}
else {
transform.rotation.x = 0;
}
Hope I helped!
EDIT: He can't change directly parts of mesh as he is doing with the head IF it's linked to bones, a RIG or something like that. To modify both the position and the speed he'll need to change the associated bone.
Your answer
![](https://koobas.hobune.stream/wayback/20220612120811im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers
How can i change vertex posiiton to push it back -1 on z ? 1 Answer