- Home /
Problem with Vector3 Distance and Mouse Input,
Hi, I have a problem with my Mouse Position. So the idea is , that I compare the position of my Mouse with the position of my gameobject. When the distnace of these two is smaller than my maximum distance variable, I destroy my gameobject. But when I play my game nothing happens and the Input isn't registered.
Here is my code:
public class Tropfen : MonoBehaviour
{
public float speed = 10.0f;
Rigidbody2D rb;
AudioSource source;
[SerializeField] AudioClip[] soundClips;
[SerializeField] GameObject particleEffect;
float isMuted;
Gameplay gameplay;
Camera mainCam;
[SerializeField] float maxDistance = 0.25f;
// Start is called before the first frame update
void Start()
{
mainCam = Camera.main;
rb = GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(0, -speed * Time.deltaTime);
gameplay = FindObjectOfType<Gameplay>();
source = GetComponent<AudioSource>();
source.clip = soundClips[Random.Range(0, soundClips.Length)];
isMuted = PlayerPrefs.GetFloat("Muted");
if (isMuted == 0)
{
source.volume = 0;
}
else if (isMuted == 1)
{
source.volume = 1;
}
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
GetInput();
}
}
private void GetInput()
{
Vector3 mousePos = Input.mousePosition;
Vector3 worldPos = mainCam.ScreenToWorldPoint(mousePos);
var plane = new Plane(Camera.main.transform.forward, transform.position);
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out var hitDistance))
{
var worldPoint = ray.GetPoint(hitDistance);
var TropfenTouchDistance = Vector3.Distance(worldPos, transform.position);
if (TropfenTouchDistance <= maxDistance)
{
TropfenDestruction();
}
}
}
private void TropfenDestruction()
{
Destroy(gameObject);
if (isMuted != 0)
{
AudioSource.PlayClipAtPoint(source.clip, transform.position);
}
GameObject effect = Instantiate(particleEffect, transform.position, Quaternion.identity) as GameObject;
Destroy(effect, 2f);
gameplay.IncreaseScore(1);
}
}
Answer by CBV · Mar 21, 2020 at 03:57 PM
So at first theres a problem right at your Destruction Methode: You're destroying the gameobject, which has the script attached, before you check all that other stuff, so it will never happen, because the script also doesnt exist anymore.
Second: I really dont get it, why you're creating a plane, to find out, if you're mouse is in distance or not. So if you're read the documentation for a plane: https://docs.unity3d.com/ScriptReference/Plane.Raycast.html you will see, that your plane always will return false, because the ray is wrong.
If you want to find out, if your mouse is pointing at the object or not, simply use Physics.Raycast:
Vector3 mousePos = Input.mousePosition;
Vector3 worldPos = mainCam.ScreenToWorldPoint(mousePos);
if (Physics.Raycast(worldPos,mainCam.transform.forward,out var hitDistance))
{
var TropfenTouchDistance = Vector3.Distance(worldPos, transform.position);
if (TropfenTouchDistance <= maxDistance)
{
TropfenDestruction();
}
}
I used your code , but it didn't work. So I used the DrawGizmo $$anonymous$$ethod to see , where my mouse is pointing to and found out that the point , to which my mouse is pointing to ,is outside of my camera and canvas. Could it have something to do with my camera or canvas settings?
Try something like this: RaycastHit hit; Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
if(hit.gameobject.GetComponent<Tropfen>()
{
//Do stuff
}
}
Your answer
Follow this Question
Related Questions
Input.mousePosition equivalent to first finger touch? 3 Answers
Use Standard Assets Mobile Input Script 0 Answers
How can I stop the player movement from being too fast while moving diagonally in my 2D game? 2 Answers
is possible press 2 buttons at the same time Mobile touch? 0 Answers
How to use crossplatform input touchpad to control space-invader style game 0 Answers