Killable Cows - Problem #2 -
Shadow_Gengar - 16.12.2010
Hey guys.
I've got a new problem now. xD
The cows won't die. :O
When I shoot them they don't do
anything..
Here's the link:
http://pastebin.com/cgPCkVgf
PLEASE help me. xD
Re: Killable Cows - Problem #2 -
Benjo - 16.12.2010
The first thing that struck me when I looked through your script was the check to see whether a player is firing his gun in the OnPlayerKeyStateChange callback:
pawn Код:
if(newkeys == KEY_FIRE && GetPlayerWeapon(playerid) != 0 && IsPlayerSpawned[playerid] == true)
The first check (newkeys == KEY_FIRE) will pass if the player is pressing the "fire" key and nothing else. This is a problem as most people will also be holding down the "aim" key too. You need to do a bitwise AND (&) check instead of a logical AND (==) check. Change that line to:
pawn Код:
if(newkeys & KEY_FIRE && GetPlayerWeapon(playerid) != 0 && IsPlayerSpawned[playerid] == true)
As for the rest of the code, it's looking pretty nice. Although honestly I haven't quite got my head around the maths for the "has the player shot a cow" check. It's really interesting to go through though, so expect another reply from me later
AW: Killable Cows - Problem #2 -
Shadow_Gengar - 17.12.2010
Wow thanks.
Oh yeah, I'm lucky that you like it. ^^
I saw this in a MTA-Video & scripted it for SA-MP. xD
Oh yes, thanks.
Greetz from Germany. :P
AW: Killable Cows - Problem #2 -
Shadow_Gengar - 17.12.2010
Hmm the cows won't die.. ~.~
Send me a PM including your WLM-Adress ok?
Re: Killable Cows - Problem #2 -
Benjo - 17.12.2010
Hello! I was playing around with your script last night for a while, but ended up going to bed before giving you a reply, sorry! :S
Anyway, I found that the major problem causing the hits to not be registered is to do with your static definition of the "Distance" parameter during your use of the GetPosFromView here on line 74:
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
if(newkeys == KEY_FIRE && GetPlayerWeapon(playerid) != 0 && IsPlayerSpawned[playerid] == true)
{
new Float:X,
Float:Y,
Float:Z;
GetPosFromView(playerid,80.0,X,Y,Z); // <---- THIS IS THE LINE I AM TALKING ABOUT
for(new id = 0; id < MAX_COWS; id++)
{
if(ObjectToPoint(playerid,4.0,CowInfo[id][iObjectID],X,Y,Z) != 0)
{
if(CowInfo[id][IsDead] == 0)
{
KillCow(id);
}
}
}
}
return 0;
}
As far as I can tell, that value of 80.0 comes from nowhere. This means that detecting a hit on a cow is possible, but you would have to be exactly 80 meters away from the cow.
What you need to instead do is find the distance between the player and a cow, and then find the coordinates that the player is looking at for that particular distance, and then repeat for each cow (assuming that cows are going to be at different distances from the player).
The second issue is not as great, but it a delicate one! It refers to the "Distance" parameter, this time in your usage of the ObjectToPoint function here:
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
if(newkeys == KEY_FIRE && GetPlayerWeapon(playerid) != 0 && IsPlayerSpawned[playerid] == true)
{
new Float:X,
Float:Y,
Float:Z;
GetPosFromView(playerid,80.0,X,Y,Z);
for(new id = 0; id < MAX_COWS; id++)
{
if(ObjectToPoint(playerid,4.0,CowInfo[id][iObjectID],X,Y,Z) != 0) // <---- THIS IS THE LINE I AM TALKING ABOUT NOW
{
if(CowInfo[id][IsDead] == 0)
{
KillCow(id);
}
}
}
}
return 0;
}
The value of 4.0 is basically saying "the object you are checking has the size 4x4x4, so check anywhere inside a 4x4x4 box". Through trial and error, I attempted to work out the closest cubic dimensions of the cow object and came up with the value 2.6.
That's about the point I got up to! The script was working in a rough manner, the cows were dying when I pointed in their general direction at least! I came to the conclusion that the ObjectToPoint function itself is the next thing to look at; just a bit of fine tuning I think! Possibly to allow for objects that are not cubic (ie cows!).
One other thing I noticed through putting SendClientMessage debug messages throughout the OnPlayerKeyStateChange callback, is that a player can spam-click the fire button to hit cows instead of only being able to hit them when a bullet is actually shot. Also, holding down shoot to fire will only do one check to see if cows are hit, even if the weapon is firing multiple shots. These aren't major issues right now, but something to bear in mind for when you have hit detection perfect
Anyway, here is the code that I was left with after I had finished:
http://pastebin.com/VmMDCVdX
I've just gone through it and tried to comment the changes I made and why I made them. I hope that it will help you further develop it!