• 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
Question by clondike7 · Oct 09, 2011 at 02:31 AM · rotationmovementphysicsspeed

Rotation used as movement

Hi everybody. I'm trying to setup a control scheme using only object rotation. If the object rotates on Z, it moves forward/backward on X. If it rotates on X, it moves on Z.

Imagine someone trying to balance a broomstick on their hand. If it tilts forward, the person must move forward to keep it in balance. That's the general idea I'm going for. I'm a good artist but very new at scripting, so I'm probably going about this the worst way possible. Here's what I have so far:

 var rotSpeed = 5.0;
 var traSpeed = 0.5;
 
 function Update () {
 
     var rotX = Input.GetAxis("Mouse Y") * Time.deltaTime * rotSpeed;
     var rotZ = Input.GetAxis("Mouse X") * Time.deltaTime * rotSpeed;
     
     var traX = rotZ * traSpeed;
     var traZ = rotX * traSpeed;
     
     transform.Rotate(rotX,0,rotZ);
     transform.Translate(traX,0,traZ);

}

Comment

People who like this

0 Show 0
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

4 Replies

· Add your reply
  • Sort: 
avatar image

Answer by wccrawford · Oct 11, 2011 at 01:27 PM

You're just looking for general advice? You don't have any actual issues with your code so far?

It looks like you've got the right idea to me, and I don't see how it could be any simpler.

If it really was like a broomstick, it'd be a lot more complicated, though. To get the broomstick upright again, you have to actually outpace the stick for a short time to make it be upright again.

Comment

People who like this

0 Show 0 · 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

Answer by clondike7 · Oct 11, 2011 at 08:43 PM

Thanks for replying!

"To get the broomstick upright again, you have to actually outpace the stick for a short time to make it be upright again."

Absolutely no idea what that means, sorry. As it is, the rotation input is directly tied to the translate, which makes the character move pretty freely and not in the direction of the rotation. I also need the character to move in a continuous direction as long as it is tilted in that direction. I hope that clears things up a bit.

Comment

People who like this

0 Show 2 · 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 wccrawford · Oct 11, 2011 at 10:01 PM 0
Share

I've stopped and put this into a test project, and it appears my 3d math isn't good enough for this yet.

I think I can give some tips, though:

You want to store the tilt and re-use and or subtract from it, instead of just using the mouse axis raw. Something like this.

var rotSpeed = 5.0;

var traSpeed = 0.5;

var tiltX = 0;

var tiltZ = 0;

function Update () {

 tiltX += Input.GetAxis("Mouse Y");

 tiltZ += Input.GetAxis("Mouse X");



 var rotX = tiltX * Time.deltaTime * rotSpeed;

 var rotZ = tiltZ * Time.deltaTime * rotSpeed;



 var traX = tiltX * Time.deltaTime * traSpeed;

 var traZ = tiltZ * Time.deltaTime * traSpeed;



 transform.Rotate(rotX,0,rotZ);

 transform.Translate(traX,0,traZ);

}

avatar image clondike7 · Oct 11, 2011 at 10:19 PM 0
Share

I'll play around with this idea. I hadn't thought of storing the tilt as variables. Thanks for the tip!

avatar image

Answer by aldonaletto · Oct 11, 2011 at 11:21 PM

If you want to do something like a Segway, that moves to the side you tilt, you can't use Rotate - Rotate will spin the object continuously in the direction you're going. You should set the eulerAngles and calculate the velocity proportionally to the joystick input:

 var traSpeed = 6.0;
 var maxAngle = 20.0;

 function Update () {
     // get the value of the axes (-1..+1)
     var rotX = Input.GetAxis("Vertical");
     var rotZ = Input.GetAxis("Horizontal");
     // set the eulerAngles
     transform.eulerAngles = Vector3(rotX * maxAngle, 0, -rotZ * maxAngle);
     // calculate the displacement (frame rate independent)
     var traX = rotZ * traSpeed * Time.deltaTime;
     var traZ = rotX * traSpeed * Time.deltaTime;
     // and move the object
     transform.Translate(traX, 0, traZ, Space.World);
 }

Comment

People who like this

0 Show 4 · 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 clondike7 · Oct 12, 2011 at 12:14 AM 0
Share

Segway! That's a much better analogy for the kind of control I'm going for. And yes, I was having the "endless rotation" problem, I was looking for a way to limit it. I am researching eulerAngles as we speak, actually.

I tried your code, but it only moves on XZ axis, no rotation. Dumb question: how do I set eulerAngles?

Thanks for the help!

avatar image aldonaletto · Oct 12, 2011 at 01:47 AM 0
Share

My fault: you're using Mouse X and Y, not a joystick! I had the joystick in my mind, and even writing Mouse X and Mouse Y I didn't notice that! I'll edit the answer.

avatar image aldonaletto · Oct 12, 2011 at 03:15 AM 0
Share

I modified the script in my answer to use the Vertical and Horizontal axes: you can use a joystick or the keys A-D (Horizontal) and W-S (Vertical).
Using Mouse X / Y is more complicated, because the values returned are way smaller, and depend on the mouse velocity. If you really need to use Mouse, define a "gain" variable, multiply by the values returned and clamp between -1 and +1:

var gain = 5.0; // adjust the sensibility here

function Update(){ rotX = Mathf.Clamp(gain Input.GetAxis("Mouse Y"), -1, 1); rotZ = Mathf.Clamp(gain Input.GetAxis("Mouse X"), -1, 1); ... The result isn't good because the values read vary a lot, making the character unstable.

avatar image clondike7 · Oct 12, 2011 at 05:21 AM 0
Share

The WASD will work for now. I'll take this small victory and study your code to better understand it (I hope). I'll tackle the Mouse conversion tomorrow. Thanks so much! I'll work on the mouse conversion tomorrow.

avatar image

Answer by clondike7 · Oct 20, 2011 at 05:34 PM

Hey guys. I'm almost 90% of the way there and wanted to share. I got the mouse controls to work, up=forward, down=backward, left and right. However, if I do a circle, which combines movements, it tends to get stuck or stutter for a sec or so. Here's what I got:

 var mousePos : Vector2 = Input.mousePosition; //Mouse position
 var maxX = Screen.width/2; //Changes mouse position on X
 var maxZ = Screen.height/2; //Changes mouse position on Y
 
 var mouseDistance : Vector2 = mousePos - new Vector2(maxX, maxZ);
 var rotX = mouseDistance.x / maxX;
 var rotZ = mouseDistance.y / maxZ;
 
 transform.eulerAngles = Vector3(rotZ * maxAngle, 0, -rotX * maxAngle);
 
 var traX = rotX * traSpeed * Time.deltaTime;
 var traZ = rotZ * traSpeed * Time.deltaTime;
 
 var controller : CharacterController = GetComponent(CharacterController);
 var moveDir : Vector3 = Vector3(traX, 0, traZ);
 controller.Move(moveDir);
Comment

People who like this

0 Show 0 · 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.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How not to make a RigidBody not go into ground when tilted foward? 1 Answer

How to make a RigidBody not go into ground when tilted foward? 2 Answers

Best way to have GameObjects reach various targets at the same time 1 Answer

Character still walking when no key is already pressed 1 Answer

Object doesn't turn at the desired rate 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