- Home /
This question was
closed Apr 29, 2020 at 02:20 PM by
kianniek for the following reason:
The question is answered, right answer was accepted
This post has been wikified, any user with enough reputation can edit it.
Question by
kianniek · Jan 26, 2019 at 02:19 PM ·
3dobjectmouse-drag
I try to move my 3D object in the game but it only on the X & Z axis, can someone help me with that
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragObject : MonoBehaviour
{
private Vector3 mOffset;
private float mZCoord;
void OnMouseDown()
{
mZCoord = Camera.main.WorldToScreenPoint(
gameObject.transform.position).z;
mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
}
private Vector3 GetMouseAsWorldPoint()
{
Vector3 mousePoint = Input.mousePosition;
mousePoint.z = mZCoord;
return Camera.main.ScreenToWorldPoint(mousePoint);
}
void OnMouseDrag()
{
transform.position = GetMouseAsWorldPoint() + mOffset;
}
}
Comment
Best Answer
Answer by tormentoarmagedoom · Jan 26, 2019 at 02:42 PM
Good day.
Add him a rigidboy component,
Freeze Position