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
1
Question by C-Blunt · May 26, 2012 at 05:49 PM · rotationmovementaxisfixed

Player moving/rotating along a single axis

Hi, I'm making a scrolling doodle-jump style game with a spaceship. I have added a rotation script to my ship to make the ship roll while turning left/right but I can't get it to work correctly.

The ship moves upwards along the Y axis and left to right alonge the x axis, i dont want it to move along the z axis at all but it seems to arc as the ship rotates...Any ideas?

using UnityEngine; using System.Collections; public class Playermovement : MonoBehaviour { public float Speed = 16.0f; //left/right movement speed private Quaternion localRotation; public float speed = 16.0f; // left/right movement speed public float rollSpeed = 5.0f; public float rollAmount = 10.0f;

 void Update() {
     rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0);
 
     transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * Speed, 0, 0);

     Vector3 dir = Vector3.zero;  //Android controls
     dir.x = -Input.acceleration.y;
     dir.z = Input.acceleration.x;
     if (dir.sqrMagnitude > 1)
         dir.Normalize();
     
     dir *= Time.deltaTime;
     transform.Translate(dir * Speed);

     // first update the current rotation angles with input from acceleration axis
     localRotation.y += Input.acceleration.x * speed;
     localRotation.x += Input.acceleration.y * speed;
     rollAmount *= 0.9f;

     if (Input.GetKey("left")) {
         rollAmount += rollSpeed ;
     } else if (Input.GetKey("right")) {
         rollAmount -= rollSpeed ;
     }

     transform.Rotate(0, rollAmount*Time.deltaTime, 0);
     rigidbody.freezeRotation = true;
 }  

 // Use this for initialization
 void Start (){
    // copy the rotation of the object itself into a buffer
    localRotation = transform.rotation;
 }

}

Thanks!

Comment
Add comment · Show 2
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 You! · May 26, 2012 at 06:05 PM 0
Share

You don't want the ship to move on the z axis...but you have "dir.z = Input.acceleration.y"... That might be your problem. You can also use a Vector2 ins$$anonymous$$d of a Vector3 since you only want to deal with the x and y axes

avatar image Phoenixst · May 26, 2012 at 06:45 PM 0
Share

You mean as your spaceship turns left on a 2D plane , you rotate it to face a certain direction , while doing so you want it to tilt to give a feeling of it turning?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · May 26, 2012 at 07:56 PM

There's an error in these lines:

     localRotation.y += Input.acceleration.x * speed;
     localRotation.x += Input.acceleration.y * speed;

localRotation is a quaternion, and their x,y,z,w components have nothing to do with the familiar angles we see in the Rotation field in the Inspector - this field is actually transform.eulerAngles. It's a very common mistake, which most people do at first (me included).
In order to keep your logic, you should declare localRotation as a Vector3, and store transform.eulerAngles in it at Start - like below:

using UnityEngine; using System.Collections; public class Playermovement : MonoBehaviour { public float Speed = 16.0f; //left/right movement speed private Vector3 localRotation; // declare localRotation as a Vector3 public float speed = 16.0f; // left/right movement speed public float rollSpeed = 5.0f; public float rollAmount = 10.0f;

 void Update() {
     rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0);
 
     transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * Speed, 0, 0);

     Vector3 dir = Vector3.zero;  //Android controls
     dir.x = -Input.acceleration.y;
     dir.z = Input.acceleration.x;
     if (dir.sqrMagnitude > 1)
         dir.Normalize();
     
     dir *= Time.deltaTime;
     transform.Translate(dir * Speed);

     // first update the current rotation angles with input from acceleration axis
     localRotation.y += Input.acceleration.x * speed;
     localRotation.x += Input.acceleration.y * speed;
     rollAmount *= 0.9f;

     if (Input.GetKey("left")) {
         rollAmount += rollSpeed ;
     } else if (Input.GetKey("right")) {
         rollAmount -= rollSpeed ;
     }

     transform.Rotate(0, rollAmount*Time.deltaTime, 0);
     rigidbody.freezeRotation = true;
 }  

 // Use this for initialization
 void Start (){
    // copy the rotation of the object itself into a buffer
    localRotation = transform.eulerAngles;
 }

} But you're modifying the Z position in the "Android controls" section, as @You (not you!) said - maybe you (not @You - God, this is becoming very confusing!) should use dir.y instead of dir.z in that section.

Comment
Add comment · Show 1 · 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 C-Blunt · May 28, 2012 at 06:50 PM 0
Share

Thanks for the help so far, I have tried what you suggested but im not sure i explained myself correctly.

The ship moves upwards along the Y axis at all times(though the camera is static, following the ship at all times, the spaceship stays at bottom-middle of screen), gaining speedboosts from pickups, you tilt the phone/use L/R arrows to move left and right along the X axis in 2-D style. I just need the ship to bank to the left/right whilst still pointing in the same direction (up on Y axis). The ship should never move along the Z axis as the pickups spawn at 0,0,0 then upwards along the Y axis, if the player drifts into the Z axis you can't get the pickups.

Thanks again

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

fixing rotation on a specific axis 1 Answer

Problem changing 3rd Person controller rotation 1 Answer

Camera rotation around a single axis - following a rolling ball 3 Answers

Moving an object on a local axis of another object 0 Answers

Is a Rotating World fesable 2 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