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 (); down = GameObject.Find ("down").GetComponent (); left = GameObject.Find ("left").GetComponent (); right = GameObject.Find ("right").GetComponent (); 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 ().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); } } ` `