• 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Camera Scrolling just track the Charakter if z-Rotation=180 0 Answers

2D Sprite Rotation 0 Answers

Unity Car Tutorial Rotate Snippet Chaos 0 Answers

Set rotation based on where player is facing 1 Answer

Counter is not working correctly with LeanTween 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges