- Home /
how to add z axis rotation on my camera with mouse scrollwhell as rotater?
i have fly throught script like in this link.
but it doesnt have Z axis rotation like what i need in my camera. what should i do to set Z axis rotation on mouse scrollwheel?
thanks:)
Comment
Best Answer
Answer by clunk47 · Feb 07, 2014 at 07:53 PM
Tested and working.
#pragma strict
var lookSpeed = 15.0;
var moveSpeed = 15.0;
var rotationX = 0.0;
var rotationY = 0.0;
var rotationZ = 0.0;
function Update ()
{
rotationX += Input.GetAxis("Mouse X")*lookSpeed;
rotationY += Input.GetAxis("Mouse Y")*lookSpeed;
rotationZ += Input.GetAxis("Mouse ScrollWheel")*lookSpeed;
rotationY = Mathf.Clamp (rotationY, -90, 90);
transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
transform.localRotation *= Quaternion.AngleAxis(rotationZ, Vector3.forward);
transform.position += transform.forward*moveSpeed*Input.GetAxis("Vertical");
transform.position += transform.right*moveSpeed*Input.GetAxis("Horizontal");
}
Answer by Ashish Dwivedi · Feb 07, 2014 at 04:04 AM
using UnityEngine;
using System.Collections;
public class Rotation : MonoBehaviour
{
float rotationZ;
// Use this for initialization
void Start ()
{
rotationZ = 0;
}
// Update is called once per frame
void Update ()
{
rotationZ = Input.GetAxis("Mouse ScrollWheel");
transform.localRotation = Quaternion.AngleAxis(rotationZ, Vector3.forward);
}
}
Add these 2 lines of update to your code and you will achieve whatever you want.
really sorry, where exactly do I have to add the code that you give to the code that I get?
var lookSpeed = 15.0;
var moveSpeed = 15.0;
var rotationX = 0.0;
var rotationY = 0.0;
var rotationZ = 0.0;
function Update ()
{
rotationX += Input.GetAxis("$$anonymous$$ouse X")*lookSpeed;
rotationY += Input.GetAxis("$$anonymous$$ouse Y")*lookSpeed;
rotationY = $$anonymous$$athf.Clamp (rotationY, -90, 90);
rotationZ = Input.GetAxis("$$anonymous$$ouse ScrollWheel");
transform.localRotation = Quaternion.AngleAxis(rotationZ, Vector3.forward);
transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
transform.position += transform.forward*moveSpeed*Input.GetAxis("Vertical");
transform.position += transform.right*moveSpeed*Input.GetAxis("Horizontal");
}
Change your code with the above code segment. This would help you...
i have try change my code with your last code. but i still can't rotate my camera in Z axis.