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 /
This question was closed Nov 10, 2013 at 12:30 PM by MadJohny for the following reason:

Question is off-topic or not relevant

avatar image
-1
Question by MadJohny · Nov 09, 2013 at 12:51 PM · c#mouseforcetargetswipe

Throw with mouse

Hi, for my game I need to be able to throw a ball using mouse, basically what I wanted was something like this:

-When you are holding the left mouse button, the mouse would stop moving

-Then you would use your mouse to use the force you want, and also where to aim, kinda like those android games where you swipe the screen with your finger and the ball goes where you have swiped to with force depending on the swipe velocity

-Also, if the force was less than the minimum force asked, the ball wouln't be thrown

I tried to achieve a similar thing to that, but I failed, anyway here's my script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour {
 
     float force;
     public float forceMultiplier;
     float forceToApply;
     public float minimumForce;
     public Rigidbody ball;
     MouseLookScript cameraScript;
 
     void Awake (){
         cameraScript = GetComponent<MouseLookScript>();
     }
     
     void Update () {
         if(Input.GetMouseButton(0)){
             force = Input.GetAxis("Mouse Y") * forceMultiplier;
             cameraScript.enabled = false;
             if(Input.GetMouseButtonUp(0)){
                 forceToApply = force;
                 if(forceToApply >= minimumForce) {
                     var ballInstance = Instantiate(ball,transform.position,transform.rotation) as Rigidbody;
                     ballInstance.rigidbody.AddRelativeForce(new Vector3(0f,0f,forceToApply));
                     forceToApply = 0f;
                     cameraScript.enabled = true;
                 }
                 else{
                     forceToApply = 0f;
                     cameraScript.enabled = true;
                 }
             }
         }
     }
 }

Help me making this script please, thanks in advance.

edit: kinda like this game: https://play.google.com/store/apps/details?id=com.dreamappslab.trickyshot

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

1 Reply

  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Nov 09, 2013 at 01:40 PM

Idealy, you would capture a click, throw a ray out and see if it hit a collider attached to the object you were on. "if(hit.transform.root.transform == transform)" This way you could have a more complex object.

You then define a drag plane and turn gravity off for the object. Don't apply force to the object, simply change the velocity to move towards the drag point. This will ideally set you up correctly. Then when you lift your mouse up, simply stop capturing and turn gravity back on. This will leave the velocity changes in tact.

 private var isDragging : boolean = false;
 private var dragPlane : Plane;
 private var moveTo : Vector3;
 
 var dragDamper : float = 5.0;
 var addToY : float = 5.0;
 
 function Update(){
 
     var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
     var dist : float;
 
     if(Input.GetMouseButtonDown(0)){
         if(Physics.Raycast(ray, hit)){
             if(hit.transform.root.transform == transform){
                 isDragging = true;
                 rigidbody.useGravity = false;
 
                 // defined drag plane:
                 // either directional based on the camera
                 //dragPlane = new Plane(-ray.direction.normalized, hit.point);
                 // or spacial based on the current position + addToY
 
                 dragPlane = new Plane(Vector3.up, transform.position + Vector3.up * addToY);
 
             }
         }
     }
 
     if(isDragging){
         var hasHit = dragPlane.Raycast(ray, dist);
 
         if(hasHit){
             moveTo = ray.GetPoint(dist);
 
         }
     }
 
     
 
     if(Input.GetMouseButtonUp(0) && isDragging){
 
         isDragging = false;
         rigidbody.useGravity = true;
 
     }
 }
 
 function FixedUpdate(){
 
     if(!isDragging) return;
 
     var velocity = moveTo - transform.position;
     rigidbody.velocity = Vector3.Lerp(rigidbody.velocity, velocity, dragDamper * Time.deltaTime);
 
 }
 
 @script RequireComponent(Rigidbody)
Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

16 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 avatar image avatar image avatar image avatar image

Related Questions

destroy object by using line renderer 1 Answer

Distribute terrain in zones 3 Answers

Apply force towards mouse click 2D C# 0 Answers

Mouse Swipe and Code Efficiency 0 Answers

How to move an object using mouse just like the swipe in iOS? 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