- Home /
Question by
onuregeunaldi1 · Feb 21, 2020 at 08:39 AM ·
c#3drotate objectmouse-drag
Rotating the game object with direction of mouse move
Hello I am preparing a 3D isometric game I need to move and rotate my 3d game object. I can move my object on a fixed y position but I need to rotate my objects With the direction of mouse move here is the code that I have;
using UnityEngine;
using System.Collections;
public class Drag : MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;
void Update()
{
if (Input.GetMouseButton(0))
{
Plane plane = new Plane(Vector3.up, new Vector3(0, 17, 0));
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float distance;
if (plane.Raycast(ray, out distance))
{
transform.position = ray.GetPoint(distance);
}
}
}
void OnMouseDown()
{
Vector3 mouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(mouse);
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
curPosition.y = 2f;
transform.position = curPosition;
}
}
Thanks for resolving it.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612223729im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do i Achieve mesh Extrusion? 2 Answers
Can I make a collider ignore only one other collider? 0 Answers
knockback 3d c# 1 Answer