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 HenkenPoot · Jul 11, 2019 at 12:37 PM · animationlocaltransformdirectionstrafing

How to have correct strafe animation based on the players rotation?

I'am making a third person shooter game where my camera is following but not rotating with my character. I want my character to be able to move and shoot while having the correct blended (strafe) moveAnimations. Watch this at 3:36 https://youtu.be/QbnVELXf5RQ to see what I mean. I'am using two floats based on the joysticks input to determine witch animation should play. I successfully made those two floats relative to the camera position, because of this my player is always walking away from the camera when moving forward no matter the cameras position. To have correct strafe motion u will need to know what the rotation is of player and in witch way it is moving. How can I achieve this? the joystick I'am using: up =1, down =-1, right = 1, left = -1.

 private void Update()
     {
         
         lX = JoystickLeft.Horizontal;
         lY = JoystickLeft.Vertical;
         rX = JoystickRight.Horizontal;
         rY = JoystickRight.Vertical;
         movement.Set(lX, 0f, lY);
         rotation.Set(rX, 0f, ry);
 
         Rotating();
         dirspeed = Vector2.SqrMagnitude(Joystick.Direction);
            
             movement = movementOrientationCam(); //making input relative to cam.   
         }

         void Rotating()
         {
         if (Input.touchCount > 1f)// player is pressing Ljoystick and Rjoystick? then we can Strafe. (for testing)
         {
             float i = lX * rY;                                                                                                                                                // the players left and right walkAnim are now switched when he is facing the camera.
             float ii = lY * rY;                                                                                                                                                //the players foreward and backward walkAnim are now switched when he is facing the camera.
             // But when he is facing left or right relative to the cam rY becomes 0 so (lX * 0 or lY * 0 = 0f) therfore he wont be playing his strafe left or right walkAnim when moving up or down (while facing left or right relative to cam).
             anim.SetFloat(xhash, i);
             anim.SetFloat(yhash, ii);
             
             if (rotation != Vector3.zero)
             {
                 Quaternion newrotation = Quaternion.LookRotation(rotation);
                 playerRig.MoveRotation(newrotation);}
              }
         else
         { if(movement != Vector3.zero)
             {
                 Quaternion newrotation = Quaternion.LookRotation(movement);                                                  //No strafing just rotate towards ur moving.
                 playerRig.MoveRotation(newrotation);}
            }
          }

     private Vector3 movementOrientationCam()
     {
         if (CameraTransform != null)
         {
             Vector3 dir = CameraTransform.TransformDirection(movement);                                            //making input relative to cam. But I still dont know how this function is working/caculating it, anyone?
             dir.y = 0;
 
             return dir.normalized * movement.magnitude;
         }
         else
         {
             CameraTransform = Camera.main.transform;
             return movement;
         }
     }
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
1

Answer by MounirDev · Jul 25, 2019 at 02:28 PM

i don't really have an idea but this section of unity can help you maybe, it's in the animator, it's where you can choose animation for your character based on the way he's walking, no need for a lot of coding, just do more research maybe you'll find what you're asking for !alt text


untitled.png (68.3 kB)
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 HenkenPoot · Jul 25, 2019 at 02:34 PM 0
Share

Yes I have tried that! But it only works if that camera is rotating with the player, otherwise if the camera is standing still and the player rotates to face the camera is goes like a mirror: left for the player becomes right for the camera. @$$anonymous$$ounirDev

avatar image MounirDev HenkenPoot · Jul 25, 2019 at 02:40 PM 0
Share

i see ! i don't know, maybe add a second camera rotating with the player and just ignore it, just to play animation lol, good luck bro.

avatar image
0

Answer by I_Am_Err00r · Jul 24, 2019 at 02:40 PM

I've seen this question edited or reposted by you for over a week now, if nobody can/wants to help, respect that and maybe seek a different solution, but just reposting this over and over and over is redundant, especially as it has been uncommented or unanswered for as long as I have noticed this question.


Unity has a standard assets package which has a third person controller in it, and I have used it and it behaves just like every other third person game I've played (GTA, Red Dead, Metal Gear, Dark Souls, etc) and that should at least give you an idea of a working third person script that you can reference for the one you are writing.

If you want another option, for a third party asset that is free, the reviews on this one look good and is another option if the Unity third person controller script isn't pointing you in the right direction.


If you're new to coding, use the free assets available because they will save you a ton of time and help you out if you still need to customize. For example, if you wanted to build a piece of furniture, you wouldn't go to a forest, chop down a tree, remove the bark, process the wood; then go to a mine and prospect for iron or whatever and smelt that to make nails, combine the wood and smelted iron to make a hammer, etc., etc., etc.; you would just go to the store and buy some processed wood, a hammer, and nails and then build from there; treat your code the same way and make it easy when you can and if you need to customize or add flare, do that to something existing and make that existing code yours.

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 HenkenPoot · Jul 25, 2019 at 01:48 PM 0
Share

Thank you for responding. I was afraid nobody would understand my question that's why I edited this question a lot. Its true that buying a hammer would speed up the process however for a beginner it is hard to understand how that hammer is made and can actually take a long time. In addition u don't know if the hammer is rust free etc. so if anything goes wrong u can't even put exactly ur finger on it. Since Hammer and nails are essential tools for making whatever, u better know what nails ur using before the hole building falls apart. Because understanding all function, making sure unwanted calculation are not made etc. takes some time making it urself wouldn't take so much more, especially in my case where it is more a mathematical puzzle. But I understand the point ur trying to make. @I_Am_Err00r

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

277 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

Related Questions

why playing animation resets object's transform in Mecanim? 1 Answer

Can I make animations snap to a frame? 1 Answer

Animation Keeps Playing In Global Coordinates 2 Answers

How to select an animation clip by index number? 7 Answers

Turn left animation. snaps back 2 Answers


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