Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Paul_Conway · May 29, 2011 at 11:14 AM · rotateaxismoveanglefixed

Move and Rotate on a fixed axis?

Hello all.

I'm working on a 2D game. The player controlled object is movable left and right on the X axis. I have also set it up that the rotation on the X axis is controlled via "Mouse X" movement.

What I want, is for the object to be locked on the X axis while being able to move and rotate at the same time. Currently, if I move after I rotate at all, the object moves off in the direction that it's rotated to.

I also need the object to have movement restrictions, so that you can't move it off screen (playing on a fixed 2d plain) and the rotation should be limited to certain angles.

Here is my basic code at the moment.

 var HorizontalSpeed : float = 2.0;
 
 function Update () {
 
 var v : float = HorizontalSpeed * Input.GetAxis ("Mouse X");
 var horiz : float = Input.GetAxis("Horizontal");
  
 transform.Translate(Vector3(horiz,0,0));
 transform.Rotate ( 0, v, 0);
 
 }

Any help would be greatly appreciated!

Thanks in advance.

Paul

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Wolfram · May 29, 2011 at 12:38 PM

By default, transform.Translate() (and Rotate(), too) modify the local space, not the world space. So instead you should use:

 transform.Translate(Vector3(horiz,0,0),Space.World);
 

http://unity3d.com/support/documentation/ScriptReference/Transform.Translate.html

On the other hand, if you want to add restrictions, you should set position and angle directly, instead of using the relative Translate()/Rotate() methods. This way, you have more control over the resulting values. For example:

 currX=transform.position.x; // get current value
 currX+=horiz; // add movement
 transform.position.x=Mathf.Clamp(currX,minX,maxX)); // make sure it stays within a given interval

You can do something similar for rotation (using transform.eulerAngles), but you need to take care of wraparound effects: if you set transform.localEulerAngles.y=-10, next time you query the value, it will return 350, not -10. You have to take this into account when restricting this to certain angles. One way to do this, which however only works if you don't modify the the transform's rotation anywhere else, is to query the value only at the beginning, and then always refer to that value and never query transform.localEulerAngles.y, only setting it.

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 Paul_Conway · May 29, 2011 at 04:31 PM 0
Share

Thanks for the help, Wolfram.

The world space fixed the issue of the object flying off and is now fixed on the correct axis.

With the restrictions, I'm having a little trouble getting it to work. I have fairly limited scripting knowledge which doesn't help!

 function Update () {
 
 var horiz : float = Input.GetAxis("Horizontal");
 
 currX=transform.position.x;
 currX+=horiz;
 transform.position.x=$$anonymous$$athf.Clamp(currX,8,-8); 
 
 }

Not sure if I edited the right fields (I tried numerous variations) but what happens when I hit play, is that the object pings between 8 and -8 on the X axis very quickly. When I press left or right key, it sticks on -8 until I let go. Obviously it needs to start at 0, then be movable via horizontal keys, on the X axis until it reaches 8 and -8, at which it can't go any further. I'm sure you can spot what I'm doing wrong very quickly! Had a look through the documentation and have been rattling my brain for an hour now, but no luck :(

Thanks again.

avatar image Wolfram · May 29, 2011 at 11:41 PM 0
Share

In the Clamp() method, the smaller limit needs to come first, so use:

 transform.position.x=$$anonymous$$athf.Clamp(currX,-8,8); 
avatar image Paul_Conway · May 30, 2011 at 10:24 PM 0
Share

Works brilliantly, thanks Wolfram!

Now to just figure out how to get that tilt limit working.

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

Rotating character based on joystick angle, at an angle 0 Answers

Wrong Camera axis after "zoom" to GameObject 0 Answers

Objects rotate but remain on the original axis... 0 Answers

Moving an object based on the position of another object 2 Answers

Axis are not rotated as expected 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