Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Pedro12909 · Feb 08, 2017 at 11:36 PM · rotationmovementrigidbody

Can't find a way to make object rotate and move at the same time.

Hi, I'm fairly new to Unity and I'm having some troubles with my first project. Basically, I have a block that slides in the z axis whenever I press the Left/Right arrows or A/D keys. I also want to be able to rotate it by it's x axis by pressing either the Q/E keys.

The only problem I'm facing is that it either moves or rotates, and I want it to be able to do both at the same time.

Here's a quick video of the project: https://i.gyazo.com/cc3d61b6b357d85eaf1c6d3ee5384411.mp4

And here's my object's movement script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BlockMovement : MonoBehaviour {
 
     public GameObject cube;
 
 
     public float speed;
     public float rotationSpeed;
 
     public float minRotation = -45f;
     public float maxRotation = 45f;
 
     void Update () {
         Vector3 moveTo = new Vector3(0f, 0f, Input.GetAxis("Horizontal") * speed * Time.deltaTime);
         cube.GetComponent<Rigidbody>().MovePosition(cube.GetComponent<Transform>().position + moveTo);
 
         if (Input.GetKey(KeyCode.Q)) {
             cube.GetComponent<Transform>().Rotate(Vector3.right * rotationSpeed * Time.deltaTime);
         } else if (Input.GetKey(KeyCode.E)) {
             cube.GetComponent<Transform>().Rotate(-Vector3.right * rotationSpeed * Time.deltaTime);
         }
     }
 }
 

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 Pengocat · Feb 09, 2017 at 12:00 AM 0
Share

Is the Rigidbody on the cube set to kinematic? If not then you should use forces within FixedUpdate to move it.

avatar image Pedro12909 Pengocat · Feb 09, 2017 at 12:05 AM 0
Share

Yes, It is

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Pengocat · Feb 09, 2017 at 12:32 AM

Note that you don't need to get the Transform of a gameobject since every gameobject has a transform component by default.


Since your cube has a kinematic Rigidbody you also don't need to move it with the Rigidbody Component. You can simply use the normal Transform.

 using UnityEngine;
 
 public class BlockMovement : MonoBehaviour
 {
     public GameObject cube;
     public float speed = 50f;
     public float rotationSpeed = 50f;
     public float minRotation = -45f;
     public float maxRotation = 45f;
 
     void Update()
     {
         float deltaTime = Time.deltaTime;
 
         float horizontalInput = Input.GetAxis("Horizontal");
         if (horizontalInput != 0f)
         {
             float deltaMovement = horizontalInput * speed * deltaTime;
             cube.transform.Translate(new Vector3(0f, 0f, deltaMovement));
         }
 
         bool isTurningLeft = Input.GetKey(KeyCode.Q);
         bool isTurningRight = Input.GetKey(KeyCode.E);
         if (isTurningLeft && !isTurningRight)
         {
             cube.transform.Rotate(Vector3.left * rotationSpeed * deltaTime);
         }
 
         else if (!isTurningLeft && isTurningRight)
         {
             cube.transform.Rotate(Vector3.right * rotationSpeed * deltaTime);
         }
     }
 }
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 Pedro12909 · Feb 09, 2017 at 03:40 PM 0
Share

That worked flawlessly! Thanks a lot! :)

Ps.: I just had to add "Space.World" at the end of both Translate and Rotate functions to ensure that they were using the world's axis ins$$anonymous$$d of the gameObject's one.

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

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

Related Questions

Smooth Rotation on WASD keys pressed? 1 Answer

After added a rigidbody in a GameObject, it moves on rotation 2 Answers

Input AnyKey only working for one of my players? 0 Answers

i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers

Why does the player not move around planet with gravity ? 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