- Home /
Drag and throw object in 3D
Hi guys, I need help with small task. I need to touch the screen, drag object and then throw it out. You can see what I need here at 3:13 DRAG PLAYING CARD - VIDEO
At the moment I can touch object and move throught screen but still few problems remaining:
throw it if touchPhase.Ended in proper direction and speed
rotate object during moving
If you have any ideas, please let me know, because I am out of ideas :)
know should't be there GetMouseButton(0), but it is just test version in laptop and I'll change it later. And sorry for grammar ;)
using UnityEngine; using System.Collections;
public class MainScript : MonoBehaviour { private float dist; private Transform toDrag; private bool dragging = false; private Vector3 offset; private Vector3 v3;
void Update() { if (Input.GetMouseButtonDown (0)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); if (Physics.Raycast (ray, out hit)) { if (hit.transform.gameObject == GameObject.FindGameObjectWithTag ("card")) { toDrag = hit.transform; dist = hit.transform.position.y - Camera.main.transform.position.y; v3 = new Vector3 (-Input.mousePosition.x, -Input.mousePosition.y, dist); v3 = Camera.main.ScreenToWorldPoint (v3); offset = toDrag.position - v3; dragging = true; } } } if(Input.GetMouseButton(0)){ if (dragging) { v3 = new Vector3 (-Input.mousePosition.x, -Input.mousePosition.y, dist); v3 = Camera.main.ScreenToWorldPoint (v3); toDrag.position = v3 + offset; } } if (Input.GetMouseButtonUp (0)) { dragging = false; } } }
Put a collider on the object, and use the standard DragRigidbody.js script.
Your answer
Follow this Question
Related Questions
Sensitivity on Android Input.acceleration 2 Answers
How to Move the Car With Touch? 1 Answer
Move to touch position 1 Answer
Android Sliding Controls 0 Answers
Tween move from CSV works on Editor but not on Android 0 Answers