- Home /
Question by
karthees · Nov 05, 2014 at 09:10 AM ·
rotationgameobjecttransform
How to rotate the game object
I have done the drag the game object in the camera applying mesh collider but how to rotate my gameobject.
using UnityEngine;
using System.Collections;
public class MyDragBehaviour : MonoBehaviour
{
private float maxPickingDistance = 2000;// increase if needed, depending on your scene size
private Transform pickedObject = null;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
foreach (Touch touch in Input.touches)
{
Debug.Log("Touching at: " + touch.position);
//Gets the ray at position where the screen is touched
Ray ray = Camera.main.ScreenPointToRay(touch.position);
if (touch.phase == TouchPhase.Began)
{
Debug.Log("Touch phase began at: " + touch.position);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, maxPickingDistance))
{
pickedObject = hit.transform;
}
else
{
pickedObject = null;
}
}
else if (touch.phase == TouchPhase.Moved)
{
Debug.Log("Touch phase Moved");
if (pickedObject != null)
{
Vector2 screenDelta = touch.deltaPosition;
float halfScreenWidth = 0.5f * Screen.width;
float halfScreenHeight = 0.5f * Screen.height;
float dx = screenDelta.x / halfScreenWidth;
float dy = screenDelta.y / halfScreenHeight;
Vector3 objectToCamera =
pickedObject.transform.position - Camera.main.transform.position;
float distance = objectToCamera.magnitude;
float fovRad = Camera.main.fieldOfView * Mathf.Deg2Rad;
float motionScale = distance * Mathf.Tan(fovRad/2);
Vector3 translationInCameraRef =
new Vector3(motionScale * dx, motionScale * dy, 0);
Vector3 translationInWorldRef =
Camera.main.transform.TransformDirection(translationInCameraRef);
pickedObject.position += translationInWorldRef;
}
}
else if (touch.phase == TouchPhase.Ended)
{
Debug.Log("Touch phase Ended");
pickedObject = null;
}
}
}
}
Comment
@karthees, can you explain your question more?
For simple rotate
gameObject.transform.Rotate(0,0,45);
For automatic rotation
void Update ()
{
transform.Rotate (0, 0,1);
}
I'm doing augmented REality project with vuforia. I'm download 3d model loading. I have the modal below then how can i rotate my model with two finger or hand
GameObject modalClone = Instantiate( modal ) as GameObject; //clone the model
modalClone.transform.localScale += new Vector3(250,250,250);
modalClone.transform.localPosition = new Vector3(0,0,0);