- Home /
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);
}
}
Answer by djenningsais · Oct 21, 2017 at 07:07 PM
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
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));
}