- Home /
How to drag and throw 2D objects?(SOLVED)
I have a script for dragging objects with my mouse while click1 is pressed but I don't know how to throw them on release.I want the objects to be thrown at the same speed I move the mouse while I'm dragging it. I want to make a character who has special powers kind of like The Force(star wars) but in 2D.
This is what I use for dragging:
void OnMouseDrag()
{
Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
point.z = gameObject.transform.position.z;
gameObject.transform.position = point;
Screen.showCursor = false;
}
void OnMouseUp()
{
Screen.showCursor = true;
}
!SOLVED!
Look below for answer!
Answer by newbieguy · Jan 16, 2014 at 10:15 PM
I found a video that shows how I would like the dragging to be.
Solved!
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public SpringJoint2D spring;
void Awake()
{
spring = this.gameObject.GetComponent<SpringJoint2D>(); //"spring" is the SpringJoint2D component that I added to my object
spring.connectedAnchor = gameObject.transform.position;//i want the anchor position to start at the object's position
}
void OnMouseDown()
{
spring.enabled = true;//I'm reactivating the SpringJoint2D component each time I'm clicking on my object becouse I'm disabling it after I'm releasing the mouse click so it will fly in the direction i was moving my mouse
}
void OnMouseDrag()
{
if (spring.enabled = true)
{
Vector2 cursorPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);//getting cursor position
spring.connectedAnchor = cursorPosition;//the anchor get's cursor's position
}
}
void OnMouseUp()
{
spring.enabled = false;//disabling the spring component
}
}
Values for SpringJoint2D Component: Distance: 0.005(minimum) Damping Ratio: 0 Frequency:1
I also changed the Ridibody2D Linear Drag value to 1 so the spring oscilation would stop.
I have't played too much with the values but the ones above are ok for me.
How I got to this solution?: I posted a video on youtube above showing Dragging Physics handled in Corona SDK, so i searched for the .lua file and saw they were using a Joint to do it.
Wait I am developing game where I must use drag in 2D ok!
After 30 $$anonymous$$utes or 1 hour I will write you !
how can i use part of ur script to drag and throw objects in a 2D game, like mario picking up turtle shell...
There's an error in line 30 if (spring.enabled = true) Should be if (spring.enabled == true) anyways Thank You very $$anonymous$$uch! :)
Answer by Vardan Meliksetyan · Jan 17, 2014 at 08:28 AM
if( Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
RaycastHit hit;
if( Physics.Raycast(ray, out hit) )
{
Debug.Log( hit.point );
firstClickPosition = hit.point;
}
// here your code "firstClickPosition is your click position"
}
Answer by Keyserjaya99 · Oct 08, 2016 at 08:31 AM
void OnMouseDrag() {
Vector3 MousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
MousePos.z = 1f; //Optional
transform.position = MousePos;
}
void OnMouseUp() {
Vector3 MousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector2 dir = MousePos - transform.position;
dir.Normalize ();
GetComponent.velocity = dir * 10f;
}
Answer by davejones1 · Mar 19, 2018 at 03:35 PM
New Hi everyone, I am trying to make use of OnMouseDrag to limit the speed at which a UI slider can be moved. The slider is used to scrub through an animation. I want to limit the speed at which the UI slider can be moved so that the animation is played smoothly.
Your answer
Follow this Question
Related Questions
New Pointer Effector 2D Help 0 Answers
Gravity Direction Change 2D 1 Answer
2D swinging physics. 0 Answers