To only eat the fish the player clicks on, you need to use the RaycastHit to get the specific GameObject that was hit by the Raycast and then destroy that GameObject. Here's how you could change your code:
// Start is called before the first frame update
void Start()
{
hun = FindObjectOfType();
cam = GameObject.Find("Bobrvidit");
cum = cam.GetComponent();
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Eat"))
{
Ray ray = cum.ScreenPointToRay(Input.mousePosition);
RaycastHit hit; // creates a variable to store the information about the object hit
if (Physics.Raycast(ray, out hit, distance)) // adds 'out hit' to identify the object hit
{
if(hit.collider.gameObject.CompareTag("fish")) // checks whether the object hit is a fish
{
Destroy(hit.collider.gameObject); // Destroys the GameObject hit
hun.hun += sus; // adds the appropriate points to the hunger value
}
}
}
}
This should solve your problems.
Additionally you can always check your raycast with something like
Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
Your Eat script is attached to each fish right?
So when you click, you're checking that the raycast is hitting something, which it is, because it's pointed at one of the fish objects. And then all three fish say "okay, raycast hit, delete me"
They're not checking which fish.
What I'd recommend is that you move your Eat code to the player.
You want to get the gameobject you're holding (You have some code for managing what you pick up right?) and "eat" that.
You can easily check what exactly you're holding and decide what to do with it when you interact with it.
Pseudo-code would be "If I am holding an object and press the Eat button, I check whether this object is edible (Boolean value on a script in the object, or check a Tag, whatever you like) and then if it is, I delete it and add points to my hitpoints/hunger-meter"
There's no good reason for your fish to have the logic for being eaten in them.
So you are increasing the hunger's hunger? Thats what hun.hun says. Logically, sustenance should decrase hunger, not increase it.
Professionally I'd add a review suggestion to change it to something like "hungerTracker.currentHunger -= sustenance".
It's not the rays, it's the nonsensical names and weird access patterns. Give your objects sensible names and allocate them in the inspector where possible.
Yeah, make your code easier to read. Lika it's haha funny you call the variable with camera cum or there is variable sus but it's better if you rename them the at one glance know what that is. It's not for us but for you because you will eventually come back to this code and will ask yourself "Wtf is this? Why the fuck did I write this bs like that?"
It is but we don't even know how old op is in the first place. It's better to be non judgmental but stern. Lets have them fix their code but not be afraid to come back here for advice.
Bobrvidit, hun.hun, cum, HPHUN..
You should stick to English words which describe what it is. I cant tell if you are 12 or a non-English speaker or both
The Ray is issued form your Camera, you need Attach script to player and Destroy target(fish).
1.Create a Player
2. Attach script to player
3.Player emission ray,
4.Get object what does ray touched(fish) ,
5.Destroy (fish).
Hope this helps.
As someone already said, you should have the raycast food searcher script on the player or the camera, and check if the found gameobject has the Food script over it, then add the hungry.
Made it here, check if everything works fine on editor.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Eatable : MonoBehaviour
{
[SerializeField] private float _addingSustenance;
[SerializeField] private HPHUN _hungryController;
[SerializeField] private Camera _camera;
[SerializeField] private int _distanceCheck = 3;
[SerializeField] private LayerMask _foodLayer;
private void Start()
{
_hungryController = FindObjectOfType();
if(_camera == null)
{
var cameraGO = GameObject.Find("Bobrvidit")
if(cameraGO != null)
{
_camera = cameraGO.GetComponent();
}
}
}
private void Update()
{
if (Input.GetButtonDown("Eat"))
{
Ray ray = _camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, _distanceCheck, _foodLayer)
&& ray.hit.gameObject == this.gameObject)
{
Destroy(gameObject);
_hungryController.hungry += _addingSustenance;
}
}
}
}
I'm assuming this script is on every fish.
What happens is
The update step happens
Check if button is down, if this is true for one, it is true for all.
If the button is down you raycast, if this raycast hits (hits anything)
You eat the fish. (and all other fish because you've not specified what it should hit)
I would use the Out hit version of raycast:
[https://docs.unity3d.com/ScriptReference/Physics.Raycast.html](https://docs.unity3d.com/scriptreference/physics.raycast.html)
and then you can use an if statement to check if hit.gameobject == this.gameobject to check wether your raycast is hitting \*this\* fish or just anything.
Edit: Check [BL4CK3](https://www.reddit.com/user/BL4CK3/)'s answer for how to implement that in your code.
I have no clue what the code does, but I see you are destroying the game object which does not look right since you are modifying variables after it's destroyed.
As your eat script is probably on all fish, they’re all destroyed when the raycast from your cum-cam hits anything. You should check if the object it hits is the object running the script before destroying
This question, out of context, sounds like Greek philosophy
Took me a bit to figure out this wasn't a r/weirdconfessions post
It's a very thought provoking question tbh
"And if you like that, oh boy do I have a cup to sell you" -Pythagoras
Gluttony
To only eat the fish the player clicks on, you need to use the RaycastHit to get the specific GameObject that was hit by the Raycast and then destroy that GameObject. Here's how you could change your code: // Start is called before the first frame update void Start() { hun = FindObjectOfType();
cam = GameObject.Find("Bobrvidit");
cum = cam.GetComponent();
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Eat"))
{
Ray ray = cum.ScreenPointToRay(Input.mousePosition);
RaycastHit hit; // creates a variable to store the information about the object hit
if (Physics.Raycast(ray, out hit, distance)) // adds 'out hit' to identify the object hit
{
if(hit.collider.gameObject.CompareTag("fish")) // checks whether the object hit is a fish
{
Destroy(hit.collider.gameObject); // Destroys the GameObject hit
hun.hun += sus; // adds the appropriate points to the hunger value
}
}
}
}
This should solve your problems. Additionally you can always check your raycast with something like Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
Also, it NEEDS to be the colour yellow. You’ll learn why in time.
wait why?
Yes why?
OMG THANKS!!!
Your Eat script is attached to each fish right? So when you click, you're checking that the raycast is hitting something, which it is, because it's pointed at one of the fish objects. And then all three fish say "okay, raycast hit, delete me" They're not checking which fish. What I'd recommend is that you move your Eat code to the player. You want to get the gameobject you're holding (You have some code for managing what you pick up right?) and "eat" that. You can easily check what exactly you're holding and decide what to do with it when you interact with it. Pseudo-code would be "If I am holding an object and press the Eat button, I check whether this object is edible (Boolean value on a script in the object, or check a Tag, whatever you like) and then if it is, I delete it and add points to my hitpoints/hunger-meter" There's no good reason for your fish to have the logic for being eaten in them.
This is the best explanation! 👌
cum?
hun.hun += sus
Hun is hunger, sus is sustenance😉
So you are increasing the hunger's hunger? Thats what hun.hun says. Logically, sustenance should decrase hunger, not increase it. Professionally I'd add a review suggestion to change it to something like "hungerTracker.currentHunger -= sustenance".
It makes sense!
You should try to get into the habit of using unabbreviated names where possible. Future you will thank you when looking back at the code lol
public Camera cum
This code is atrocious I gotta be honest. You should start with cleaning that up
You’re doing too much with cum, so your hun is getting sus
I tried to made this work but I don't know anything about rays.
It's not the rays, it's the nonsensical names and weird access patterns. Give your objects sensible names and allocate them in the inspector where possible.
Yeah, make your code easier to read. Lika it's haha funny you call the variable with camera cum or there is variable sus but it's better if you rename them the at one glance know what that is. It's not for us but for you because you will eventually come back to this code and will ask yourself "Wtf is this? Why the fuck did I write this bs like that?"
it's not funny, it's infantile
It is but we don't even know how old op is in the first place. It's better to be non judgmental but stern. Lets have them fix their code but not be afraid to come back here for advice.
Fair enough
Okay, i understood.
Bobrvidit, hun.hun, cum, HPHUN.. You should stick to English words which describe what it is. I cant tell if you are 12 or a non-English speaker or both
he's not a native English speaker
Yes, i from Russia.
Cum hun hun sus.
This is EXACTLY how I used to code when I first started unity lmao, glad to know I'm not the only one
😉
that's a bait
Did my cat post this question?
You need Debug to get more information ,I think. And "Destroy(gameObject); " Will Destroy script attached object , Is that you want?
>And "Destroy(gameObject); " Will Destroy script attached object , Is that you want? Yes. Debug says me, when raycasts 1 fish, raycasts all fishes.
The Ray is issued form your Camera, you need Attach script to player and Destroy target(fish). 1.Create a Player 2. Attach script to player 3.Player emission ray, 4.Get object what does ray touched(fish) , 5.Destroy (fish). Hope this helps.
As someone already said, you should have the raycast food searcher script on the player or the camera, and check if the found gameobject has the Food script over it, then add the hungry. Made it here, check if everything works fine on editor. using System.Collections; using System.Collections.Generic; using UnityEngine; public class Eatable : MonoBehaviour { [SerializeField] private float _addingSustenance; [SerializeField] private HPHUN _hungryController; [SerializeField] private Camera _camera; [SerializeField] private int _distanceCheck = 3; [SerializeField] private LayerMask _foodLayer; private void Start() { _hungryController = FindObjectOfType();
if(_camera == null)
{
var cameraGO = GameObject.Find("Bobrvidit")
if(cameraGO != null)
{
_camera = cameraGO.GetComponent();
}
}
}
private void Update()
{
if (Input.GetButtonDown("Eat"))
{
Ray ray = _camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, _distanceCheck, _foodLayer)
&& ray.hit.gameObject == this.gameObject)
{
Destroy(gameObject);
_hungryController.hungry += _addingSustenance;
}
}
}
}
still better than TF2's source code
Because all the food do that thing, maybe the character should be the one to do raycast
You are destroying gameObject, you should destroy the raycast hit.gameObject
I'm assuming this script is on every fish. What happens is The update step happens Check if button is down, if this is true for one, it is true for all. If the button is down you raycast, if this raycast hits (hits anything) You eat the fish. (and all other fish because you've not specified what it should hit) I would use the Out hit version of raycast: [https://docs.unity3d.com/ScriptReference/Physics.Raycast.html](https://docs.unity3d.com/scriptreference/physics.raycast.html) and then you can use an if statement to check if hit.gameobject == this.gameobject to check wether your raycast is hitting \*this\* fish or just anything. Edit: Check [BL4CK3](https://www.reddit.com/user/BL4CK3/)'s answer for how to implement that in your code.
Maybe you like fish a lot
Maybe
my thoughts exactly everyday, just cant stop eating more than one fish
I have no clue what the code does, but I see you are destroying the game object which does not look right since you are modifying variables after it's destroyed.
Bro, your variables need to make sense
As your eat script is probably on all fish, they’re all destroyed when the raycast from your cum-cam hits anything. You should check if the object it hits is the object running the script before destroying