Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 W1k3 · Nov 25, 2013 at 05:02 AM · rotationtransformvector3quaternionlerp

How to smoothly rotate to certain directions using input axis

Kind of a big question here.

I have a space ship. I want it to face different directions based on the arrow keys/wasd. I don't mean look straight to the left or straight up, but on an angle. So if my ship is facing forward and I press up, the ship would loop up at a 45 degree angle and straight when I let go.

That would seem kinda simple, but I need it to work for up down left right as well as when you hold down left and up and so on so forth. I tried creating 8 empty objects around the ship and using look at to determine where the ship is facing, but it just flips out and goes crazy.

There has to be a better way. I have a hard time grasping unity rotation, but what I basically need is to store 8 different rotations for an object and smoothly switch between them depending on what buttons I'm pressing.

Any help would be awesome.

Edit: I'm using C#

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

Answer by robertbu · Nov 25, 2013 at 06:34 AM

If you have the the "Horizontal" and "Vertical" axes setup in their default configuration, you can get the rotation you are looking for with just a few lines of code:

 #pragma strict
 
 var speed = 5.0;
 
 function Update () {
 
     var v3 = Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 1.0);
     var qTo = Quaternion.LookRotation(v3);
 
     transform.rotation = Quaternion.Slerp(transform.rotation, qTo, speed * Time.deltaTime);
 }
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 W1k3 · Nov 25, 2013 at 11:34 PM 0
Share

Awesome, I get what it means, but is there any chance you might know what this is in C#?

avatar image robertbu · Nov 25, 2013 at 11:44 PM 0
Share

Untested C# conversion:

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$yClass : $$anonymous$$onoBehaviour { 
     public float speed = 5.0f;
     
     void Update () {
         
         Vector3 v3 = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 1.0f);
         Quaternion qTo = Quaternion.LookRotation(v3);
         
         transform.rotation = Quaternion.Slerp(transform.rotation, qTo, speed * Time.deltaTime);
     }
 }
avatar image W1k3 · Nov 25, 2013 at 11:46 PM 0
Share

This is working great! Thanks allot.

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

17 People are following this question.

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

Related Questions

Instantiate GameObject towards player 0 Answers

Transform a Vector by a Quaternion 1 Answer

Unity Quaternion Problem (Should be A Bug?) 1 Answer

Make a side of an object LookAT another object 1 Answer

Need to check transform.rotation 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