Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Chocolade · Apr 27, 2017 at 06:34 PM · c#scripting problemscript.

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.

Original Ik Active not checked

But this is what i'm getting when checking the checkbox on Ik Active: The flashlight is facing up:

Flashligh 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 ? )

ss128.jpg (431.4 kB)
ss129.jpg (429.7 kB)
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

1 Reply

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

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?

Comment
Add comment · Show 2 · 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 DJT4NN3R · Apr 27, 2017 at 06:49 PM 0
Share

This is the solution I typically use.

avatar image Igor_Vasiak · Apr 27, 2017 at 06:57 PM 0
Share

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

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

323 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

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


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