Skip to content

Instantly share code, notes, and snippets.

@seferciogluecce
Last active December 10, 2021 23:46
Show Gist options
  • Save seferciogluecce/e57dd9e884bd38d2925f3de7826f5dd4 to your computer and use it in GitHub Desktop.
Save seferciogluecce/e57dd9e884bd38d2925f3de7826f5dd4 to your computer and use it in GitHub Desktop.
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Collider))]
public class DragAndShoot : MonoBehaviour
{
private Vector3 mousePressDownPos;
private Vector3 mouseReleasePos;
private Rigidbody rb;
private bool isShoot;
void Start()
{
rb = GetComponent<Rigidbody>();
}
private void OnMouseDown()
{
mousePressDownPos = Input.mousePosition;
}
private void OnMouseUp()
{
mouseReleasePos = Input.mousePosition;
Shoot(mouseReleasePos-mousePressDownPos);
}
private float forceMultiplier = 3;
void Shoot(Vector3 Force)
{
if(isShoot)
return;
rb.AddForce(new Vector3(Force.x,Force.y,Force.y) * forceMultiplier);
isShoot = true;
}
}
@EduardMalkhasyan
Copy link

thanks !! your the best !

@ozzcet
Copy link

ozzcet commented Aug 15, 2021

Hi my friend,
Thanks a lot to share such a thing. It will open the gate for us. I just want to ask you, when i fallow your video and use this code, the ball is throwed on the ground , it did not jump to wall. Why it may happend? Best regards.

@a2937
Copy link

a2937 commented Dec 10, 2021

It appears a Unity bug makes this impossible to work with if the camera is majorly rotated. I found out this issue when I had my camera's y value set to -90 degrees.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment