- Home /
How to Rotate .fbx image in Unity3d
I am new to Unity 3D,what i want to do is rotate a 3d .fbx image of tshirt with mouse,please tell me step by step method in c# to do this?
Based on what inputs: mouse, touch, keyboard?
Is the rotation confined to one axis?
If mouse, is this a drag situation or is it following the mouse at all times?
Rotation of objects has been covered extensively on UA. What answers/scripts have you tried and what problems did you have?
mouse 2.)it should be 3d rotation,so all the three axis 3.)it should follow mouse
Answer by robertbu · Apr 28, 2014 at 04:53 PM
A mouse is a 2D device, so you cannot follow the mouse in 3D. What you can do is use the mouse movement as an implied rotation around an axis, which is probably what you are looking for. Here is a bit of code to get you started. It requires that your tshirt have a mesh collider, so you will need to add a mesh collider if it does not have one (Component > Physics > Mesh Collider). It only works when the camera is stationary and looking towards positive 'z' (the default setup for the Main Camera). Attach the script to the object you want to rotate.
using UnityEngine;
using System.Collections;
public class RotateObject_6 : MonoBehaviour {
private Vector3 startPos;
private float factor = 100.0f;
void OnMouseDown () {
startPos = Input.mousePosition;
}
void OnMouseDrag () {
Vector3 direction = Input.mousePosition - startPos;
Vector3 axis;
axis.y = -direction.x;
axis.x = direction.y;
axis.z = 0.0f;
gameObject.transform.RotateAround(axis, direction.magnitude/factor);
startPos = Input.mousePosition;
}
}
Answer by mgsteinkamp · Apr 28, 2014 at 04:55 PM
If you import Character Controllers from the Standard Assets, there is a script in there called "MouseLook"
If you attach that to an Asset, it will rotate based on the mouse. You could then just make it conditional by encasing everything within the update function with "
if(Input.GetButton("Fire1") == true){
//update code
}
" and you will have an object that you can rotate by clicking with the left mousebutton and then dragging.