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 MrSplosion · Jan 30, 2011 at 11:26 PM · rotationlocation

Location and Rotation not Working

I'm simply trying to move an object on the local Z axis with the W and S keys, so I used transform.localPosition instead of transform.position which gave me the same result :/. The other problem is the rotation which only rotates for like 3 seconds then stops why doesn't this move my object on the local axis and what is wrong with the rotation?

var speed : float = 0.1;

function Update(){ //forward if(Input.GetKey("w")){ transform.localPosition.z += speed; } //backward if(Input.GetKey("s")){ transform.localPosition.z += -speed; } //left if(Input.GetKey("a")){ transform.rotation.y += -speed; } //right if(Input.GetKey("d")){ transform.rotation.y += speed; } }

Thanks

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
Best Answer

Answer by tool55 · Jan 30, 2011 at 11:39 PM

transform.localPosition.z is meant to be read only or for specific placement of an object, not for translation. You're going to get very inconsistent results doing it the way you've shown above.

What you want is transform.Translate (0,0, speed*Time.deltaTime) Just change your speed variable from + to - to change the direction. There are good examples in the scripting reference.

Same thing for rotation: transform.Rotate (X,Y,Z);

The following is directly from the scripting reference:

// A very simplistic car driving on the x-z plane.

var speed : float = 10.0; var rotationSpeed : float = 100.0;

function Update () { // Get the horizontal and vertical axis. // By default they are mapped to the arrow keys. // The value is in the range -1 to 1 var translation : float = Input.GetAxis ("Vertical") speed; var rotation : float = Input.GetAxis ("Horizontal") rotationSpeed;

 // Make it move 10 meters per second instead of 10 meters per frame...
 translation *= Time.deltaTime;
 rotation *= Time.deltaTime;

 // Move translation along the object's z-axis
 transform.Translate (0, 0, translation);
 // Rotate around our y-axis
 transform.Rotate (0, rotation, 0);

}

Comment
Add comment · Show 3 · 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 Bunny83 · Jan 30, 2011 at 11:49 PM 0
Share

Actually that's not true, if position is meant to be read only they would have made it read only. The Translate function just do exactly the same thing as transform.position += transform.forward speed Time.deltaTime.

avatar image Bunny83 · Jan 31, 2011 at 12:20 AM 0
Share

You can even set the eulerAngles manually without any problems but here you have to keep the angle within the right range or it will fail. It's all explained in the scripting reference.

avatar image tool55 · Jan 31, 2011 at 12:28 AM 0
Share

Interesting, thanks for the correction.

avatar image
0

Answer by Bunny83 · Jan 31, 2011 at 12:18 AM

Two things:
First rotation or localRotation are Quaternions but you used them like eulerAngles.
As tool55 mentioned don't increase eulerAngles because setting the angles to wrong values will fail. To rotate a transform use a Quaternion or what will be easier in your case (also like tool55 said) the Rotate function

transform.Rotate(0,0,rotationSpeed * Time.deltaTime);

rotationSpeed is in degrees per sec.

If your object isn't a child localPosition equals position. That means it's the position in world space and increasing z will move it on the global z axis. If it is a child position.z will represent the position in the local space of your parent but not in your own local space.
To move along the own local-z-axis you can use transform.forward which is a vector that points in the z axis of your transform. Just use that vector and add it to your position like:

transform.position += transform.forward * moveSpeed + Time.deltaTime;

moveSpeed is in Units per sec. Which should be meters.

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

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to smoothly rotate an object through just one axis 1 Answer

My Zombie Model is coming at me sideways with the AI 1 Answer

Rotate an instantiated GameObject towards another GameObjects pivot point 1 Answer

Calc angle. 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