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
0
Question by jumach · Feb 28, 2014 at 05:27 AM · rotationquaternionrotateangleseuler

How do I rotate a game object in 45 degree steps per keystroke?

I'd Like to start by first admitting that I am extremely new to any kind of scripting and this is quite literally the first time I have ever used c#. In my scene/script I have an empty game object which is the parent of my camera and thus is the focal point for my camera. Through my script I am able to move this around having my camera follow from a fixed position. After hours of struggling and much referring to the documentation, I was able to make the camera rotate in 45 degree increments around the y-axis of the focal point in both directions at the the touch of a key. However, the issue I'm running into now is that the object continually rotates (quite fast). Ideally I would like the object to only rotate 45 degrees per keystroke. Here is an example of what I have so far:

 using UnityEngine;
 using System.Collections;
 
 public class cameraController : MonoBehaviour {
 
     public float speed;
     public GameObject camera;
     private Vector3 currentPos;
     private Vector3 currentRotation;
 
     void Update () {
 
         //Keep the camera focused on the target
         currentPos = transform.position;
         camera.transform.LookAt (currentPos);
         //currentRotation = transform.rotation.eulerAngles;
     }
 
 
     void LateUpdate () {
 
         //Horizontal Movement
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
 
         transform.position = currentPos + movement * Time.deltaTime * speed;
 
 
 
     }
 
     void FixedUpdate () {
 
         //Camera rotation around the focal point
         float rotateFloat = Input.GetAxis("RotateY");
         int rotateY = Mathf.RoundToInt(rotateFloat);
         Quaternion currentRotation = transform.rotation;
         
         
         if (rotateY > 0) {
             
             transform.rotation = currentRotation * Quaternion.Euler(0, 45.0f, 0);
             Debug.Log("1");
         }
         if (rotateY < 0) {
             
             transform.rotation = currentRotation * Quaternion.Euler(0, -45.0f, 0);
             Debug.Log("2");
         }
 
         }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by mhtraylor · Feb 28, 2014 at 06:31 AM

Use an Input.GetButton or Input.GetButtonDown method for this. The Input.GetAxis is for continuous movement, but the situation you have described is for the movement to take place in discrete steps at the press of a button or key. For instance, per the docs, Input.GetButtonDown will only return true during the frame that the button was pressed.

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 jumach · Mar 08, 2014 at 05:49 AM 0
Share

The problem with getButtonDown is that it won't allow me to differentiate between the positive and negative version of the button unless I explicitly state which keys exactly. This causes the unfortunate side-effect of making key bindings non-customizable for the end user which is not an acceptable outcome for me.

avatar image
0

Answer by NoahConstable · Mar 08, 2014 at 09:14 AM

Just call Input.GetAxisRaw("AxisName"), but add some extra code...

C#:

 private bool usingAxis = false;
  
 void Update(){
     if( Input.GetAxisRaw("AxisName") != 0){
         if(usingAxis == false){
             // Do your axis action here
             usingAxis = true;
         }
     }
     if( Input.GetAxisRaw("AxisName") == 0){
         usingAxis = false;
     }    
 }

This should work (I'll explain why in the comment section). If it does, please mark this answer as correct so others can see it and learn as well, ask their own questions, and grow the Unity community.

Happy Coding!

Noah.

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 NoahConstable · Mar 08, 2014 at 09:20 AM 0
Share

Here is the explanation for the script (by the way, "AxisName" is the name of the axis you want to use)

C#:

 //A boolean to see if we are using the axis
 private bool usingAxis = false;
  
 //Update is called every frame 
 void Update(){
     //If we get the axis "AxisName" and it is not 0 (or otherwise known as being used)
     if( Input.GetAxisRaw("AxisName") != 0){
         //Set our boolean to false
         if(usingAxis == false){
             // Do your axis action here
             
             //Set our boolean to true again
             usingAxis = true;
         }
     }
     
     //If the axis ("AxisName") is equal to zero (or otherwise known as not being used)
     if( Input.GetAxisRaw("AxisName") == 0){
     
         //Set the boolean to false
         usingAxis = false;
     }    
 }

Any questions you have, please post them. I'll be monitoring this post until someones answer is marked as correct (or you ingore for a couple of days).

Happy Coding!

Noah.

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

22 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotation like in editor 2 Answers

instant rotate an object 3 Answers

How do I rotate a quaternion with euler angles? 0 Answers

Quaternion.Slerp problem... 1 Answer

Rotation with a slider 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