Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fluckysan · Sep 13, 2012 at 10:13 AM · gameobjectmousevelocitythrow

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;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fluckysan · Sep 13, 2012 at 11:57 AM 0
Share

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 !

avatar image venhip · Sep 13, 2012 at 12:04 PM 0
Share

You're welcome. Its been my experience that the scripts that come with unity can be a nightmare to edit, so good luck!

avatar image
0

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");
                 }
             }
         }
         
     
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fluckysan · Sep 13, 2012 at 12:49 PM 0
Share

Thx for the script :)

Can you post it in a new answer ? It's pretty difficult to read or copy it !

avatar image hiramyoucan · Sep 13, 2012 at 01:44 PM 0
Share

Hi,I have updated my script^^^hoping it can do you a little favor.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast To Collect Items 0 Answers

Can I select an object in the hierarchy with code while the game is running? 1 Answer

Cannot precisly get the Transform(position and rotation) and velocity of the HTC vive controller in Unity 0 Answers

Drag GameObject only in a specific area 1 Answer

How to get local axis from characterController.velocity 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges