How to rotate a object with joystick?

Hi everyone!

I want to rotate a object with joystick, but how can I use joystick put to left, object rotate left. joystick put to right, object rotate right.
joystick put to up, object rotate up. joystick put to down, object rotate down.

But I don’t know how to do

Can any one help me?

I’m not sure if I correctly understood what you’re trying to do (at least with respect to rotate up and rotate down ), but I guess the script below can do the job. It uses Input.GetAxis to read both axes, then apply the rotation using Rotate:

var speed: float = 60; // rotate 60 degrees per second

function Update(){

  var vert = Input.GetAxis("Vertical") * speed * Time.deltaTime;
  var hor = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
  transform.Rotate(vert, hor, 0);
}

This will give a rotation like that of an airplane: the object will rotate up/down and next left/right around its local axes. If this isn’t what you’re looking for, let me know and I’ll show another method (keeping one axis universal and the other local).

EDITED: In order to make the object follow the joystick, you can use this code:

var maxVert: float = 60; // define the max vertical angle (to each side)
var maxHor: float = 60;  // define the max horizontal angle (to each side)
private var initRot: Quaternion;

function Start(){
  initRot = transform.rotation;
}

function Update(){
  var angV = Input.GetAxis("Vertical") * maxVert;
  var angH = Input.GetAxis("Horizontal") * maxHor;
  transform.rotation = initRot * Quaternion.Euler(angV, angH, 0);
}

NOTES:

1- If your object turn to the wrong side in one of the axes, apply a minus sign if front of angV or angH in the last line;

2- If your object rotates instead of inclinate to one of the sides, your axes are different from mine. You can change the axis in the last line - Quaternion.Euler(0, angH, angV), for instance. A better alternative would be to create an empty object, child your object to it, zero its position and place the script in the empty object (don’t forget to remove the script of the child object!).