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 /
avatar image
0
Question by LetsStartSomeSeriousCoding · Oct 21, 2017 at 04:00 PM · rotationscripting problembuttonrotate object

Object won't rotate

Hi, I'm doing a simple android game. I've imported a 3d model and dragged it to the scene. I attached to the main object a script to simply rotate it by touching 4 buttons, but when I press the buttons the object stands still. I tryed to move it, and it works.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class script : MonoBehaviour {

 Button up;
 Button down;
 Button left;
 Button right;
 private GameObject obj;

 float v;
 public float velocity;
 public float rotX;
 public float rotY;

 void Start () {
     obj = gameObject;
     v = 0f;
     up = GameObject.Find ("up").GetComponent<Button> ();
     down = GameObject.Find ("down").GetComponent<Button> ();
     left = GameObject.Find ("left").GetComponent<Button> ();
     right = GameObject.Find ("right").GetComponent<Button> ();
     up.onClick.AddListener (btnup);
     down.onClick.AddListener (btndown);
     right.onClick.AddListener (btnright);
     left.onClick.AddListener (btnleft);
 }

 void Update () {
     if (Input.GetKeyDown (KeyCode.Space))
         v += velocity*Time.deltaTime;
     else if (v - velocity * Time.deltaTime >= 0f)
         v -= Time.deltaTime;
     GetComponent<Rigidbody> ().velocity = new Vector3 (transform.rotation.x, transform.rotation.y, transform.rotation.z) * v;
 }
 void btnleft(){
     obj.transform.rotation = new Quaternion (obj.transform.rotation.x, obj.transform.rotation.y + obj.transform.rotation.y*Time.deltaTime*rotY, obj.transform.rotation.z, obj.transform.rotation.w);
 }
 void btnright(){
     obj.transform.rotation = new Quaternion (obj.transform.rotation.x, obj.transform.rotation.y + obj.transform.rotation.y*Time.deltaTime*rotY, obj.transform.rotation.z, obj.transform.rotation.w);
 }
 void btnup(){
     obj.transform.rotation = new Quaternion (obj.transform.rotation.x + obj.transform.rotation.x*Time.deltaTime*rotX, obj.transform.rotation.y, obj.transform.rotation.z, obj.transform.rotation.w);
 }
 void btndown(){
     obj.transform.rotation = new Quaternion (obj.transform.rotation.x - obj.transform.rotation.x*Time.deltaTime*rotX, obj.transform.rotation.y, obj.transform.rotation.z, obj.transform.rotation.w);
 }

}

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

Answer by djenningsais · Oct 21, 2017 at 07:07 PM

@LetsStartSomeSeriousCoding

In your btn* methods you are multiplying your rotation increment by the current rotation. If the current rotation is 0 (like at start) then you will never see a rotation increment.

Take out the rotation.x and rotation.y from your factor calculation and it should work fine.

Thanks for the challenge!! :)

Example for btnleft():

 obj.transform.rotation.y + Time.deltaTime * rotY

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 LetsStartSomeSeriousCoding · Oct 22, 2017 at 05:53 AM 0
Share

Thanks :'D

avatar image
0

Answer by lernoh · Oct 30, 2017 at 02:39 PM

@ LetsStartSomeSeriousCoding in case if u wish to rotate something with javascript :)

function Update () {

         if(Input.GetKey(KeyCode.UpArrow))
             transform.Translate(Vector3(0,0,-2));

         if(Input.GetKey(KeyCode.DownArrow))
             transform.Translate(Vector3(0,0,2));


         if(Input.GetKey(KeyCode.RightArrow))
             transform.Rotate(Vector3(0,2,0));

              if(Input.GetKey(KeyCode.LeftArrow))
             transform.Rotate(Vector3(0,-2,0));

             if(Input.GetKey("v"))
             transform.Translate(Vector3(0,10,0));

             if(Input.GetKey("b"))
             transform.Translate(Vector3(0,-1,0));
           

}

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

153 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 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

Controls work fine on PC, but not on android phone. 0 Answers

CCTV behavior rotation issue 2 Answers

i need to rotate cube in z axis or x axis one direction at time 0 Answers

how to make a detect rotation script for negative rotation too 2 Answers

rotate position change????? 0 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