• 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 MikezNesh · Jul 19, 2010 at 09:16 PM · 2drotation

How do I edit my code to make it work for 2d game?

I found this code online:

Right now it works well except it rotates the x axis which ruins my 3d effect. How can I keep the x axis and y axis at 0 rotation?

var accel = 0.8; var inertia = 0.9; var speedLimit = 10.0; var minSpeed = 1.0; var stopTime = 1.0;

private var currentSpeed = 0.0;

private var functionState = 0; private var accelState : boolean; private var slowState : boolean;

private var waypoint : Transform; var rotationDamping = 6.0; var smoothRotation = true; var waypoints : Transform[]; private var WPindexPointer : int;

function Start () { functionState = 0;
}

function Update () { if (functionState == 0) { Accell (); } if (functionState == 1) { Slow (); } waypoint = waypoints[WPindexPointer]; }

function Accell () { if (accelState == false) {
accelState = true;
slowState = false;
}
if (waypoint) { if (smoothRotation) { var rotation = Quaternion.LookRotation(waypoint.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation,Time.deltaTime rotationDamping); } } currentSpeed = currentSpeed + accel accel; transform.Translate (0,0,Time.deltaTime * currentSpeed);

if (currentSpeed >= speedLimit) { currentSpeed = speedLimit; } }

function OnTriggerEnter () { functionState = 1; WPindexPointer++;

if (WPindexPointer >= waypoints.Length) { WPindexPointer = 0; } }

function Slow () { if (slowState == false) {
accelState = false; slowState = true;
}

    currentSpeed = currentSpeed * inertia; 
    transform.Translate (0,0,Time.deltaTime * currentSpeed); 

if (currentSpeed <= minSpeed) { currentSpeed = 0.0; yield WaitForSeconds (stopTime); functionState = 0; } }

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

Answer by qJake · Jul 19, 2010 at 09:19 PM

At the end of Update, or in LateUpdate:

transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);

Or whatever angles you want to have set at 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 MikezNesh · Jul 19, 2010 at 09:28 PM 0
Share

is there anymore efficient ways? cuz then it gets called alot. any ways to like set it once and it works?

avatar image MikezNesh · Jul 19, 2010 at 09:32 PM 0
Share

also which one tells what number to set the axis to? cuz i want to change that value. Thanks.

avatar image qJake · Jul 19, 2010 at 09:38 PM 0
Share

What? Everything in Update "gets called a lot". One line is about as efficient as it gets. And it's X, Y, Z, the same as every other Transform function in Unity.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

2D character rotation wrong direction when moving 1 Answer

2d rotation and model orientation 1 Answer

2D sprite rotation with velocity and interpolation 1 Answer

Smooth 2D rotation unexpected 1 Answer

How to map screen coordinates to individual Tiles in a rotated Tilemap? 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