- Home /
Question is off-topic or not relevant
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
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)
Follow this Question
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