Skip to content

Instantly share code, notes, and snippets.

@fxcosta
Created September 28, 2018 12:54
Show Gist options
  • Save fxcosta/be8fb9ce4ecc2271dac135c6a7d5039e to your computer and use it in GitHub Desktop.
Save fxcosta/be8fb9ce4ecc2271dac135c6a7d5039e to your computer and use it in GitHub Desktop.
Testing script for rotate a 3d object in Unity3D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GestureHandler : MonoBehaviour {
public float rotationSpeed = 8;
public Camera cam;
// Use this for initialization
float rotSpeed = 20;
public float moveSpeed = 10f;
public float turnSpeed = 50f;
void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y")*rotSpeed*Mathf.Deg2Rad;
// giro somente pelas laterais
// float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.Deg2Rad;
// transform.RotateAround(Vector3.up, -rotX);
transform.RotateAround(transform.up, -rotX);
transform.RotateAround(Vector3.right, rotY);
// transform.Rotate(Vector3.right *rotSpeed * Time.deltaTime);
// transform.Rotate(0,5,0, Space.World);
}
void Start () {
}
// Update is called once per frame
void Update () {
// RotateObject();
// if(Input.GetKey(KeyCode.UpArrow))
// transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
// if(Input.GetKey(KeyCode.DownArrow))
// transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
// if(Input.GetKey(KeyCode.LeftArrow))
// transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
// if(Input.GetKey(KeyCode.RightArrow))
// transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
void RotateObject()
{
//Get mouse position
Vector3 mousePos = Input.mousePosition;
//Adjust mouse z position
mousePos.z = cam.transform.position.y - transform.position.y;
//Get a world position for the mouse
Vector3 mouseWorldPos = cam.ScreenToWorldPoint(mousePos);
//Get the angle to rotate and rotate
float angle = -Mathf.Atan2(transform.position.z - mouseWorldPos.z, transform.position.x - mouseWorldPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, angle, 0), rotationSpeed * Time.deltaTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment