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 Epicshark · Jan 11, 2015 at 08:08 AM · transformvector3localposition

Finding the forward vector of a child in local space?

What i'm trying to do is have a character move forward in the direction they are looking on a spaceship that is freely rotatable and will have any sort of orientation. I have the character moving in local space, and that keeps it glued to the floor, but the x and z component of the forward vector are different based on the ship's orientation. Here's my code:

 Vector3 forward = head.transform.forward;        
 headHeading = new Vector3 (forward.x, 0, forward.z);
 this.transform.localPosition += headHeading.normalized * walkSpeed;

I don't know whats actually happening, but i'm pretty sure it's because the forward vector is in world space, not local space. If i'm right about that, how do I make sure that the character does what is desired(moves in the direction the head is pointed whilst stuck to the ground, regardless of the ship's orientation)?

Here's an example: I have the ship in some crazy orientation. alt text

I should be walking forwards but instead i'm drifting around, alt text

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 tanoshimi · Jan 11, 2015 at 10:24 AM 0
Share

In scene view, set tool handles to show local rotation (the button to the right of "Pivot"), and then click on each of the Ship, Player, and Head in turn. $$anonymous$$ake sure that their transforms are all oriented correctly so that their y axis (green) is pointing up, z (blue) is forward), and x (red) is right.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by sweatyrat · Jul 15, 2015 at 06:57 PM

Vector3 forward = this.transform.parent.InverseTransformDirection(head.transform.forward);
headHeading = new Vector3 (forward.x, 0, forward.z);

this.transform.localPosition += headHeading.normalized * walkSpeed;

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
avatar image
0

Answer by tanoshimi · Jan 11, 2015 at 08:16 AM

Your forward vector is in World Space, but that's what you want. A forward vector in local space would always be (0,0,1).

The problem might be your use of localPosition. What if you use position instead?

If not, you need to post more details of your hierarchy.

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 Epicshark · Jan 11, 2015 at 09:55 AM 0
Share

$$anonymous$$y hierarchy is: the Ship is the parent, the player character is a child of the ship and the head (camera) is a child to the character. Ship > Player > Head

I've tried using position ins$$anonymous$$d of localPosition but it doesn't fix the problem. Ins$$anonymous$$d, the character is moving up and down as well since the space is no longer relative to the floor of the ship. I think this is because the Y value of the vector needs to be 0 (since I want the player to stay on the floor).

avatar image
0

Answer by Maurice_B · Jan 11, 2015 at 08:16 AM

could you use rotation instead?

 transform.rotation.y = head.rotation.y;
 tramform.Translate(0,0,walkspeed) ;
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 Epicshark · Jan 11, 2015 at 10:31 AM 0
Share

Didn't work :(

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

Locking gameobject to rotating floor 0 Answers

Class member Vector3 based on local transform 1 Answer

localposition not working? Calculating a vector in front of an object. Picture and code included 1 Answer

spawn forward 1 Answer

unknown axis of rotation 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