Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 attekym · Mar 27, 2015 at 05:47 AM · rotationmovementanimatorplayerquaternion

Animator movement and mouse position problem

Hey, I've been struggling with this problem for a few days now, and I'm starting to lose my sanity here.

The problem is that I have a character in game and an animator, with animations, attached to it. The character looks at the current mouse position, so the transform.forward is towards the mouse. The character turning works as intended but another problem arises when I try to move the player according to world coordinates instead of his current rotation.

I have this basic type of a script as for now:

 float h = Input.GetAxis("Horizontal");
 float v = Input.GetAxis("Vertical");
 
 PlayerMovement(v, h);

My PlayerMovement function is declared elsewhere, but the problem is that I want for the player to move, ie. when pressing up, to move up (z-axis +). Currently, when pressing up, the player moves towards the mouse position (transform.forward), because the animator's floats are made in such way. In the PlayerMovement function I send the h and v floats to my animator which has values according to the two of them.

What I am trying to achieve, is to convert the floats h and v, so that it takes the player's rotation into account. The character should, like I said earlier, move up when I press up, left when I press left et cetera according to world, not it's own rotation.

I hope you understand what I'm trying to achieve. For example, if you have horizontal 1.0f and vertical 0.0f coming from the player input (player is walking right), the animator floats should be 1 and 0 respectively if the player's rotation.y is equal to 0. If however, the player's rotation.y is for example 90 degrees, the animator values should be 0 and 1 (ie. the player is walking forward, because he is looking right).

I think that the two calculations (for the animator's horizontal and vertical variables) should both use the h, and v and the player's rotation. I've tried using sin and cos functions, but I still can't get my head around it. There might be some Mathf-function which will solve this easily, but I sure as hell know nothing about it.

Damn, this ended up as a wall of text, but I thank you in advance!

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

can anyone find the issue with this code? 2 Answers

How can I move an object to click point in 2D? 0 Answers

Why my player don't go forward? 0 Answers

My walking animations root motion has a problem when my mouse look script is being used? 0 Answers

Gradual Rotation of Character from 2 Axis Input Sources 0 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