Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Thunderocity · Jan 14, 2012 at 08:24 AM · directionfacingfront

How to change the "front" of my object?

Now I feel like this is a stupid question (i'm trying to learn javascript), but it's really stumped me, so here goes! When I import in a model, I can then rotate it however I like, but the script doesn't treat the visual front (it's a starship) as the front, instead it propels it sideways. Yet rotating it left and right works...

This is the code i'm dissecting:

 var life:int;
 
 var thrust:int;
 
 
 
 var rotationSpeed:float;
 
 function Update(){
 
 var h=Input.GetAxis("Horizontal");
 
 var v=Input.GetButton("Vertical");
 
 transform.Rotate(Vector3(0,h*Time.deltaTime*rotationSpeed,0));
 
 if(v){
 
 rigidbody.AddForce(transform.forward*Time.deltaTime*thrust);
 
 }
 
 }
 

Does this have something to do with local or world space? How is the "front"/"sides" of an object set, and how can I change them? I'd appreciate any help you guys can give! Thanks!!!

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 Winterblood · Jan 14, 2012 at 10:05 AM 0
Share

Please give your question a meaningful title so people know what you're asking about!

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by roamcel · Jan 14, 2012 at 08:48 AM

This issue plagues new users.

Try like this:

  • create a new empty gameobject

  • put your script on this empty gameobject

  • drag the model as a child of the empty gameobject, and orient it as it should, with its front following the world z axis (the blue axis)

All rotations that you apply to the parent will propagate to the child, regardless of its weird original rotations (that are 'normally awkward' on imported models)

Comment
Add comment · Show 5 · 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 Thunderocity · Jan 14, 2012 at 08:08 PM 0
Share

Ok, I tried that, and the result I got was so ridiculous that i'm now more confused about the script than I was before. When I added the script to the parent it changed how everything worked (I should mention i'm making a 2d game). Now, it's rotating it bottom to top ins$$anonymous$$d of left to right! The top arrow key no longer applys thrust, but if you push the top arrow and then the left or right arrows, it'll apply thrust along the x axis while rotating (in the wrong direction). How long you hold down the top arrow key seems to affect what the left and right keys do...

It's hard for me to explain, because the way it's behaving makes no sense. Somehow adding a gameobject, and a rigid body to that game object radically changed the script...? I didn't change any of the code, fyi. Thanks for the reply! $$anonymous$$aybe you know why it's going bonkers?

avatar image roamcel · Jan 15, 2012 at 06:58 AM 0
Share

Every object can be moved and rotated and scaled based on two different coordinate systems: local and global. What you're experiencing is most certainly related to the confusion that this can cause.

What you need to do is to ensure that your root gameobject has 0, 0 and 0 as rotation angles in the inspector then, if you apply a transform.translate(Vector3.right) to that object, it'll move to the right.

If ins$$anonymous$$d you rotate that object around the Z axis, so that the red arrow points left, if you apply the same translate, the object will move to WORLD right, but it'll move to ITS left!

This is really all there is to it, but it'll drive you mad.

Basically, always figure where the axes are pointing to understand if Vector3.forward, right and up point in the intended direction of movement.

To convert from local to global and viceversa (for example if you need to calculate the forward of a child in world space, you use

Vector3 myworldforward = child.transform.InverseTransformDirection(transform.forward);

For example, if the child's blue axis is pointing 'back' in world coordinates, myworldforward would equal 'Vector3.back'

Oh and I should probably insist on this: FORWARD IS THE BLUE AXIS, RIGHT IS THE RED AXIS, UP IS THE YELLOW AXIS! Orient your objects in such fashion.

I$$anonymous$$PORTANT NOTE: you $$anonymous$$UST make 100% sure that your imported model's scale is 100% 1. ANY scaling in ANY part of it will produce unwanted results in transformation operations.

avatar image Thunderocity · Jan 15, 2012 at 09:05 AM 0
Share

You can disregard my answer, it hasn't been approved by a mod yet so I can't delete it. But anyways, I used the transform.right and it worked perfectly! But you mentioned that this wasn't the correct thing to do? What's the correct way? Is that where the parent object comes into play because I still dont understand how that helps me because of the rotation problems?

Anyways, thanks for your help, really appreciate it!

avatar image roamcel · Jan 16, 2012 at 09:44 PM 0
Share

heh let's put it like this: if it works, it's the correct way to do it :D Glad you sorted it out

avatar image Nomibuilder · May 15, 2015 at 12:03 PM 0
Share

@roamcel Thank You So much. It solved my issue :-)

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I respawn my RigidBodyFPSController facing new direction? 0 Answers

Facing of game objects? 2 Answers

Circling around a target while maintaining direction of facing 0 Answers

teleport in facing direction 1 Answer

Checking to see if the player is facing an enemy? (c#) 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