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 yellowelephant · Aug 08, 2014 at 05:56 AM · rotationtransformworld spacelocal space

Rotate and transform in local space

I've got a problem with a controller i'm trying to code. The idea is, that the object faces and moves in the direction the joystick is pointing. It works as long as there is no camera movement/rotation because the whole movement of the controller is oriented at world space. I have no idea how to change that and need help.

This is my code so far.

 if (left_horizontal != 0.0 || left_vertical != 0.0) {
 var angle = Mathf.Atan2(-left_vertical, left_horizontal) * Mathf.Rad2Deg;
 transform.rotation = (Quaternion.Lerp (heroTransform.rotation, Quaternion.AngleAxis(90.0 - angle, Vector3.up), Time.time * (force * moveSpeed)));
 controller.Move(transform.forward * force * moveSpeed);
 }

Comment
Add comment · Show 2
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 rutter · Aug 08, 2014 at 05:58 AM 0
Share

Is it the rotation or the translation that's giving you trouble? Your translation call ("move forward") looks more or less okay.

It's not quite clear to me how you want this thing to rotate. If you just want to take joystick input, you'd probably use transform.Rotate. Does that do what you need? If not, why not?

avatar image yellowelephant · Aug 08, 2014 at 08:29 AM 0
Share

Thanks for your reply! Right now it is not working like it should be because the object movement (rotation and translation) is always in world space. That means if i change my camera angle and position the movement doesn't fit with the perspective seen on screen. I'm not really good at explaining the problem?!

2 Replies

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

Answer by robertbu · Aug 08, 2014 at 03:44 PM

I'm fuzzy on the details of what you want, but here is an educated guess:

 if (left_horizontal != 0.0 || left_vertical != 0.0) {
     var angle = Mathf.Atan2(-left_vertical, left_horizontal) * Mathf.Rad2Deg;
     var fwd = Camera.main.transform.forward;
     fwd.y = 0.0;
     var q = Quaternion.FromToRotation(transform.forward, fwd) * transform.rotation;
     var q = Quaternion.AngleAxis(angle, Vector3.up) * q;
     transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * rotationSpeed);
 conroller.Move(transform.forward * force * moveSpeed);
 }

If your current code is working correctly, there are still aspects that don't make sense or are wrong. For example, I don't know what 'heroTransform.rotation' is, and the use of Time.time in the Lerp() is wrong.

'rotationSpeed' is float you need to define and assign. Start with a value of 5.0.

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 yellowelephant · Aug 09, 2014 at 12:07 PM 0
Share

Thank you very much ! That did the trick. I left out any declarations of variables in my first post, so sorry for the confusion. I'm trying to wrap my $$anonymous$$d about your code and to understand what I did wrong but so far i'm just happy it works.

avatar image
0

Answer by zarawesome · Aug 08, 2014 at 12:07 PM

Well, you have to use the camera's vectors instead of global ones. Start by replacing

 Vector3.up

with

 Camera.main.transform.TransformDirection (Vector3.forward);

You might want to try TransformDirection with Vector3.up or Vector3.right instead, depending on which way your camera was originally oriented in.

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 yellowelephant · Aug 08, 2014 at 03:27 PM 0
Share

Thank you for your reply ! But unfortunately that didnt change anything of the behaviour. The object still moves in world space ins$$anonymous$$d of being relative to the camera.

EDIT: After giving it some thought I think the problem lies in the calculation of the rotation with the controller joystick. I'm not sure how to convert THAT into local space.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rigidbody constraints in local space? 7 Answers

Physics in Local Space rather than World Space 2 Answers

acceleration and eulerAngle problem 0 Answers

Move an object toward an angle in 2d space 0 Answers

Syncing Position and Rotation data in ECS tests? 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