Drag a 3-D object using mouse unity| Leran unity | Drag in unity | Unity screen to world tutorial

Subscribers:
1,110
Published on ● Video Link: https://www.youtube.com/watch?v=Zn-cZBywxSo



Category:
Tutorial
Duration: 0:42
86 views
6


In this video i have shown how to drag a 3-D object using mouse in unity
Code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*
- Attach this script on that gameObject which you want to drag.Note: That gameObject must have a
collider on it.
- This script selects and drags/moves an object in world space i.e 3-d space .
Logic:

1. Player is dragged according to position of cursor (OnMouseDrag).
*/

public class Drag : MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;


void OnMouseDown(){
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}

void OnMouseDrag(){
Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
transform.position = cursorPosition;
}


}




Other Videos By Muhammad Shahzaib


2022-06-19Police Siren in unity3d easy tutorial
2022-06-17Set JFrame title and Resize JFrame | Learn Java GUI
2022-06-15Download and install unity tutorial
2022-06-04Copy Text To Clip board Unity | #CopyClipboard in unity
2022-05-29Cloth Physics in Unity 3d | Flag Example included | Step by step easy tutorial
2022-05-21Ayat al kursi | Ayat ul kursi with beautiful background and soothing sound| #Quran
2022-04-24#Cargo Parking| #My new released game| download link in description
2022-04-22#Raycasting in unity | #Ray cast hit detection unity c# in less than 1 min | Code in description
2022-04-10Make transparent material | #Unity3d| #Transparent material in unity3d
2022-03-19HMS Gui update and add patients data entry | Other controls (Clock,alarm, cutomization,notes) added
2022-03-13Drag a 3-D object using mouse unity| Leran unity | Drag in unity | Unity screen to world tutorial
2022-03-07Rotate a game object around other Unity | Rotate Arount unity| Learn unity | Unity rotation
2022-02-16Make a custom sky/sky box from image in unity3d| Unity skybox development from scratch|Learn unity3d
2022-02-06Hospital Management System Using Java and SQL Server Demo
2022-01-30Shafqat Mehmood at GCU Lahore | Shafqat Mehmood funny speech
2022-01-24Heavy rain in punjab village| Beautiful rain falling in winter season
2022-01-23Cyclic Redundancy Check Demo | Software demo on Cyclic Redundancy Check | CRC
2022-01-16Sura e Baqara Ayat 259-260 Urdu/Hindi Translation | beautiful Quran Translation | Quran recitation
2021-09-04jawab e shikwa | Iqbal heart touching poetry| Awakening Muslims
2021-08-28Island Surival Game tutorial | Designing beautiful island / forest terrain | Adding trees and grass
2021-08-28Island Survival game Tutorial | All assets download | Links to good websites in description