Objective: Using the mouse, place the crosshairs on the orange cube. Click to pick it up. You SHOULD be able to run around with it and it stay with you. You SHOULD be able to let go of the mouse button to drop the object (turn gravity back on).
Issues:
I can pick up the object, and turn around and the orange object stays in front of me, but not if I move the player. It leaves the cube behind.
Letting go of the mouse does not turn gravity back on, so it floats.
I followed this tutorial (By Jimmy Vegas) to pick up boxes with the mouse(which worked fine)
I followed this tutorialto implement raycasting with the crosshairs, which somewhat works as the color of the object hit by the raycast and tagged as “selectable” does indeed change material color…but issues 1 and 2 above persist. This script uses an empty gameobject “SelectionManager” that handles the raycasting.
I’m fairly new to Raycasting and C#, so I’m trying to piece together different scripts…which could be the problem!
I’m afraid few, if any, people are going to look through 20 minutes of tutorial to see which code you tried to implement, without even a guarantee that that’s what you actually did, or being able to see what you might have changed.
So the least you should do is post your code as is.
There is several ways to achieve this, and we basically cant know what you did.
Edit: I just noticed the github link above the image. Quite easy to miss.
And those are quite a few scripts to look through.
This is actually a great approach to expanding your brain. But it requires careful attention to detail and data flow to really find out what is going on. Fortunately there are some great timeless techniques to help you.
Regardless of what is going on above, you must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong: