• 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 Bgjackso · Mar 25, 2017 at 08:52 AM · animationrotationquaternionlerpmelee attack

Quaternion rotation not behaving as intended

I am fairly new to Quaternions, and I seem to have run into an issue. The following script is attached to my player and the weapon GameObject is a child of the player. The motion is correct, however, the weapon object always points in 1 direction and does not follow the player's direction. How can I fix this issue? From what I can tell, my issue is in my if/else of the motion. Should I not use Lerp and something else?

 public class SwordMelee : MonoBehaviour {
 
     public float rotationAngle = 90;
     public float startingPosition = 0;
     public float smooth = 5;
     public GameObject weapon;
 
     private bool swing = false;
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         swing = false;
 
         float animation = Input.GetAxis("Vertical") * rotationAngle;
         float normal = Input.GetAxis("Vertical") * startingPosition;
         Quaternion target = Quaternion.Euler(0, -animation, 0);
         Quaternion atRest = Quaternion.Euler(0, normal, 0);
 
         if (Input.GetButton("Fire1"))
         {
             swing = true; 
         }
 
         if (swing == true)
         {
             weapon.SetActive(true);
             weapon.transform.rotation = Quaternion.Lerp(weapon.transform.rotation, target, Time.deltaTime * smooth);
             
         }
         else
         {
             weapon.SetActive(false);
             weapon.transform.rotation = Quaternion.Lerp(weapon.transform.rotation, atRest, Time.deltaTime * smooth);
             
         }
     }
 }
Comment
Add comment · Show 1
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 Namey5 · Mar 25, 2017 at 10:10 PM 0
Share

If you want it to be following the player's rotation, surely you would use localRotation ins$$anonymous$$d of rotation?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RotemAV · Mar 28, 2017 at 07:28 AM

if u want to flip you're character ,u can scale it by -1, this will flip the character and all the children

 public void Flip(float move)
     {
         if (move > 0 && !facingRight || move < 0 && facingRight)
         {
 
             facingRight = !facingRight;
             Vector3 scale = transform.localScale;
             scale.x *= -1;
             transform.localScale = scale;
            
         }
     }
Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector3.lerp giving me the incorrect Quaternion 1 Answer

Rotate object: script works if applied to that object, but not if referencing object in scene 1 Answer

How to smoothly roate a cube by 90 degrees. 2 Answers

How can I use lerp to rotate an object multiple times? 2 Answers

Quaternion Rotation On Specific Axis Issue 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges