#I want to drop when hold is false
1 messages · Page 1 of 1 (latest)
can you give more context
I can pick up an drop objects with e
But I also want drop it when magicka is < 1
So when magicka < 1 hold variable turns false
In line 60 it checks if hold is true
Even if the hold is false it continues carrying the object
bc on fixedupdate it only checks for currentobject, not for hold
send me the if condition
void FixedUpdate()
{
if (hold == true)
{
if (CurrentObject)```
pro tip: instead of nesting conditions just do if(hold == true && CurrentObject)
pro tip2: dont need to check if its true, just check the variable if(hold)
- if it still drags the object then hold is true
add a print check everytime you set it true
be sure to add the line in the print check so its easier to know where its setting true
then check where its supposed to turn false
check the conditions
It was like that before
it was not
do this
what do you mean by print check like this?
Debug.log(hold)
i mean print("hold line: n")
I didnt understand what you mean by print
print is a function
it says print does not have parameter line
send me how you wrote it
print(hold , line: 80);
It just writes hold, line:80 in console what am I going to do with that?
thats called debugging
now add one here to check if its changing
but dont use " this time
just print(hold)
under the iff statement or update
under if statement
btw is your problem hold not updating when magicka going 0 or it still dragging while hold is false
noo hold goes false
just doesnt drop it
oh alr then
still, add the print(hold) under the if statement
actually add it in update
ok so this is the line that drags your object right
actually this is
void FixedUpdate()
{
if (hold && CurrentObject)
{
Vector3 DirectionToPoint = PickupTarget.position - CurrentObject.position;
float DistanceToPoint = DirectionToPoint.magnitude;
CurrentObject.velocity = DirectionToPoint * 12f * DistanceToPoint;
}
}```
ok so if you are dragging the object it means that hold is true
meaning hold isnt false as you expected
add a print for magicka on update too
try also just pressing E to release the object and see if that works
I can drop it like that
then magicka isnt reaching a value less than one
can you tell me how much is decrease?
I can change it its 50 now
it was 20 before
when it hits like 1 hold turns false and magicka starts increasing
but doesnt drop
alr, so every frame that magicka is greater than 1 and its holding it decreases
yeah
then hold isnt turning false
variable turns false I can see it
on the line that checks if magicka is below 1 it doesnt set hold as false
in inspector?
on console
oh yea
Debug.Log(hold);
add a print("holding") below the if statement on fixedupdate
check if its beeing printed
when hold is false
also be sure youre saving your script
it always true when true and false when false on console
when you do this
you mean a new variable or
what
do this
then return to me
Oh okay now I understood why we are doing this
alr
it only wrote once
but it kept holding ingame?
I mean holding not hold
yes it kept holding
yea, i mean your character kept holding the object in game
wrote once like in the beginning
oh wait
I can drop it but I cant pick anything up later
and I need to press e one more time to set gravity to normal
so it drops when magicka is <1?
yEAH
ok, to fix this first
create a new function
in that function write this
CurrentObject.useGravity = true;
CurrentObject = null;
you're gonna call that function here and here
{
CurrentObject.useGravity = true;
CurrentObject = null;
}```
tell me when youre done and if it worked
Bro thanks you so much
it worked
but when I kill a enemy that I'm holding
it says NullReferenceException: Object reference not set to an instance of an object
because it is null I think
wait youre holding enemies on your game?
thats a fire idea
when you kill an enemy do you delete its gameobject?
yes
yea, then CurrentObject turns null
but your fixedupdate still tries to use it
yeah
do a check if its null on the end of update
if it is, hold=false
just do if(!CurrentObject)
of if(CurrentObject == null) if you prefer
np