Last active
December 13, 2024 16:46
-
-
Save seferciogluecce/e57dd9e884bd38d2925f3de7826f5dd4 to your computer and use it in GitHub Desktop.
Tutorial Video: https://youtu.be/99yIg-A5eCw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
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
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.