- Home /
Best way to throw GameObject with mouse ?
Hi,
I'm seeking for a script or a solution to grab a GameObject and throw it according to the mouse velocity (speed)
So far I'm able to grab and move the GameObject but I don't know how to throw the GameObject on the mouseUp :
private var screenPoint:Vector3;
private var offset:Vector3;
private var curPosition:Vector3;
private var isDragging:boolean = false;
function Start(){
oldMouse = Vector3.zero;
}
function FixedUpdate(){
if(isDragging){
var curScreenPoint:Vector3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
rigidbody.MovePosition(curPosition);
}
}
function 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));
rigidbody.isKinematic = true;
}
function OnMouseDrag(){
isDragging = true;
}
function OnMouseUp(){
isDragging = false;
rigidbody.isKinematic = false;
}
Answer by venhip · Sep 13, 2012 at 11:27 AM
I assume you're thinking of something like the game called paper toss? In one of the free unity packages there is a script called 'DragRigidbody.js'. This script should allow to drag the desired rigid body, and because the object will have a velocity from being dragged by the mouse as soon as you let go the rigid body should continue on its trajectory.
Hope I helped.
Yeah that's pretty my goal, I just tried and it's almost that. I'll go on this way and tweak the script, thx !
You're welcome. Its been my experience that the scripts that come with unity can be a nightmare to edit, so good luck!
Answer by hiramyoucan · Sep 13, 2012 at 12:42 PM
Hi there,I have finished this functionality a few days ago.here it is:
/********
* by hiram
* ******/
using UnityEngine;
using System.Collections;
//using System.Collections.Generic;
//using EasyMotion2D;
public class NPCMainMenu : NPCBase
{
public float fBoundray = -200.0f;//发射边界值
public GameObject mainMenuUIController;//碰撞时向其发送消息
private bool beDraggedJudge = false;//被拖拽判断
private bool positionInitJudge = true;
private float fOffsetNPC = 40.0f;//NPC偏移量
private Vector3 directionShoot;//发射方向
private Vector3 positionStartFence;//发射的起始位置(通过临界线判断NPC是否发射时)
private Vector3 positionStartNormal;//发射的起始位置(通过在临界区域内NPC是否发射时)
private Vector3 positionEnd;//发射终止位置
private Vector3 positionInput;//当前鼠标在屏幕点击点
private float positionOffset;//位置偏移量
private float timeStart;//发射起始时间
private float timeEnd;//发射终止时间
private float timeOffset;//时间偏移量
private float fTempX;
private float fTempY;
private float fTempZ;
private float positionTimeRatio;
private Vector3 initPosition;
public GameObject mainMenuController;
void Start()
{
initPosition = transform.position;
}
public override void Update()
{
base.Update();
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (!beDraggedJudge)
{
RaycastHit hit;
if (!Physics.Raycast(ray, out hit))
{ //射线结果为空
return;
}
if (null == hit.collider)
{ //碰撞体为空
return;
}
if (hit.collider.gameObject.tag == "NPC")
{
beDraggedJudge = true;
}
}
if (beDraggedJudge)
{
Debug.DrawRay(Camera.mainCamera.transform.position, ray.origin, Color.blue);
fTempX = ray.origin.x;
fTempY = ray.origin.y;
fTempZ = transform.position.z;
transform.position = new Vector3(fTempX, fTempY + fOffsetNPC, fTempZ);
if (positionInitJudge)
{
//初始化抛出的起始点坐标、时间
positionStartFence = new Vector3(fTempX, fTempY, fTempZ);
positionStartNormal = new Vector3(fTempX, fTempY + fOffsetNPC, fTempZ);
timeStart = Time.time;
positionInitJudge = false;//初始化完成后不再使用
}
//如果向下拖拽,重新发射起始位置、时间
if (positionStartFence.y > fTempY + fOffsetNPC)
{
positionStartFence = new Vector3(fTempX, fTempY + fOffsetNPC, fTempZ);
timeStart = Time.time;
}
if (positionStartNormal.y > fTempY + fOffsetNPC)
{
positionStartNormal = new Vector3(fTempX, fTempY + fOffsetNPC, fTempZ);
timeStart = Time.time;
}
//NPC超出边界,发射出去
if (transform.position.y > fBoundray)
{
beDraggedJudge = false;
//获取发射终止位置、时间
positionEnd = new Vector3(fTempX, fTempY + fOffsetNPC, fTempZ);
timeEnd = Time.time;
timeOffset = timeEnd - timeStart;
positionOffset = Vector3.Distance(positionEnd, positionStartFence);
positionTimeRatio = positionOffset / timeOffset;//位移与时间比率,用于控制发射速度
//超出临界区域调整发射速度,防止发射过快过慢
if (positionTimeRatio < 300.0f)//调整最小速度
{
positionTimeRatio = 1.0f;
}
else
{
positionTimeRatio /= 1000.0f;
if (positionTimeRatio < 1.0f)
{
positionTimeRatio = 1.0f;
}
else
{
if (positionTimeRatio > 3.0f)//调整最大速度
{
positionTimeRatio = 3.0f;
}
//其他为正常速度
}
}
m_Direction = positionEnd - positionStartFence;//起始点终止点判断发射方向
m_Direction.Normalize();
directionShoot = new Vector3(m_Direction.x, m_Direction.y, m_Direction.z);
ChangeDirection(positionTimeRatio * directionShoot);//发射出去
ChangeState(NPC_STATE.NPC_SHOOT);//改为发射状态
}
}
}
else//鼠标抬起时
{
if (beDraggedJudge)
{
positionEnd = new Vector3(fTempX, fTempY + fOffsetNPC, fTempZ);
timeEnd = Time.time;
timeOffset = timeEnd - timeStart;
positionOffset = Vector3.Distance(positionEnd, positionStartFence);
positionTimeRatio = positionOffset / timeOffset;
if (transform.position.y >= -150)
{
m_Direction = new Vector3(0, 1, 0);
m_Direction.Normalize();
directionShoot = new Vector3(m_Direction.x, m_Direction.y, m_Direction.z);
ChangeDirection(positionTimeRatio * directionShoot);//发射出去
ChangeState(NPC_STATE.NPC_SHOOT);//改为发射状态
beDraggedJudge = false;//结束拖拽状态
}
//临界区域内抛拽发射,调整速度。
if (positionTimeRatio > 300.0f)//调整最小速度
{
positionTimeRatio /= 1000.0f;
if (positionTimeRatio < 1.0f)
{
positionTimeRatio = 1.0f;
}
else
{
if (positionTimeRatio > 3.0f)//调整最大速度
{
positionTimeRatio = 3.0f;
}
}
m_Direction = positionEnd - positionStartFence;//起始点终止点判断发射方向
m_Direction.Normalize();
directionShoot = new Vector3(m_Direction.x, m_Direction.y, m_Direction.z);
ChangeDirection(positionTimeRatio * directionShoot);//发射出去
ChangeState(NPC_STATE.NPC_SHOOT);//改为发射状态
beDraggedJudge = false;//结束拖拽状态
}
else//速度太小,不发射
{
transform.position = new Vector3(fTempX, fTempY, fTempZ);//由发射状态重新回到鼠标点击点
}
}
}
if ((transform.position.x > 340) || (transform.position.x < -315) || (transform.position.y > 450))
{
mainMenuController = GameObject.FindWithTag("NPCMainMenuController");
mainMenuController.SendMessage("CreateNewCow", initPosition);
Destroy(this.gameObject);
}
}
void OnTriggerEnter(Collider other)
{
mainMenuUIController = GameObject.FindWithTag("UIController");
if (other.gameObject.tag == "MainMenuPlay")
{
mainMenuUIController.SendMessage("Invoke_Play");
}
if (other.gameObject.tag == "MainMenuSurvive")
{
mainMenuUIController.SendMessage("Invoke_SurviveBtn");
}
}
}
Thx for the script :)
Can you post it in a new answer ? It's pretty difficult to read or copy it !
Hi,I have updated my script^^^hoping it can do you a little favor.