- Home /
a strange problem ?
Hi ...
I'm trying to make a FPS game , but I have a problem :
I used this code "Physics.Linecast(transform.position,Target.position,hit)" to let the solider know if he can or can't see the player ..
the problem when I go in front of the solider he will shoot me ,and between the first and the second shoot about 0.2 seconds . now if I turn the player ( his back to solider ) and walk to the solider he will attack me in very very speed ,he's will not stop for 0.2 as first ..
were is the problem ...
thanks in advance...
link of the soldier script : http://www.mediafire.com/?b5qe0uzy3bq6nbb
this video about the problem : http://www.mediafire.com/?83n6p44uanqb4dv
Soldier.js (Added by alucardj):
function Shoot() {
var hit : RaycastHit;
SoilderEye = Vector3(transform.position.x,0.5,transform.position.z);
if ( (ActionOfAttacking == WaitAction.AttackWhenInCircle)&&(Physics.Linecast(SoilderEye,Target.position,hit))
&& (hit.collider.gameObject.tag=="Players")&&(Vector3.Distance(transform.position,Target.position)<SeeTargetDistance) )
{
ShootingWhenSee();
}
else if ( (ActionOfAttacking == WaitAction.AttackChangePlaces)&&(Physics.Linecast(SoilderEye,Target.position,hit))
&& (hit.collider.gameObject.tag=="Players")&&(Vector3.Distance(transform.position,Target.position)<SeeTargetDistance) )
{
ShootingChangePlaces();
}
}
function ShootingWhenSee() {
var forward = transform.forward;
var targetDir = Target.position - transform.position;
var angle = Vector3.Angle(targetDir , forward);
//-----< Shoot At Target >-----//
if (angle > SeeTargetAngle)
{
//-----< Rotate To The Target >-----//
var targetPoint = Target.position;
var targetRotation = Quaternion.LookRotation (targetPoint - transform.position,Vector3.down);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation,Time.deltaTime * 25);
transform.rotation.x = 0;
transform.rotation.z = 0;
AimAndShoot();
}
}
function AimAndShoot() {
var hit1 : RaycastHit;
var direction = transform.TransformDirection(Vector3.back);
if ( ShootingTimer > 0 )
ShootingTimer --;
else
ShootingTimer = ShootingSpeed;
if ((ShootingTimer==ShootingSpeed)&&( Physics.Raycast(ShootPos, direction, hit1, 15) ) )
{
var newSight;
if(hit1.collider.gameObject.tag =="Players")
{
//print("Player got shooted");
//var newBlood = Instantiate(Blood,hit1.point, Quaternion.identity);
//newBlood.transform.rotation = Quaternion.FromToRotation(Vector3.down, hit1.normal);
}
else
if(hit1.collider.gameObject.tag =="Cars")
{
var newSpark = Instantiate(Spark,hit1.point, Quaternion.identity);
newSpark.transform.rotation = Quaternion.FromToRotation(Vector3.down, hit1.normal);
newSight = Instantiate(SightOfBullet,hit1.point, Quaternion.identity);
newSight.transform.rotation = Quaternion.FromToRotation(Vector3.down, hit1.normal);
newSight.parent = GameObject.Find("AllBullitSights").transform;
}
}
currentanimation = "shoot";
if (Muzzle1.renderer.enabled)
{
Muzzle1.renderer.enabled = false;
Muzzle2.renderer.enabled = false;
Muzzlelight.transform.GetComponent(Light).enabled = false;
}
else
{
Muzzle1.renderer.enabled = true;
Muzzle1.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
Muzzle2.renderer.enabled = true;
Muzzlelight.transform.GetComponent(Light).enabled = true;
audio.clip = SoilderSounds.Shoot;
audio.Play();
}
}
How is the linecast related to the behaviour you're describing? You didn't post any question relevant code... So how are we supposed to help?
btw, i've deleted your duplicate question.
Please edit your question, paste your code and FOR$$anonymous$$AT it there, then clean up this comment.
How are these called? From Update? I don't see where you are looking for any delay. How is ShootingTimer set, and reset?
sorry about that comment....
this link of the solider script http://www.mediafire.com/?b5qe0uzy3bq6nbb
this video about the problem : http://www.mediafire.com/?83n6p44uanqb4dv
Ins$$anonymous$$d of posting stuff on mediafire, why don't you post the code here and the videos on Youtube? It makes it much easier for us
Answer by Bunny83 · Jun 30, 2012 at 10:26 AM
It's simply that you don't have a "ShootingTimer". You just have a "ShootingCounter" which counts down by one each frame. Your framerate is probably higher when the other model isn't rendered, therefore your "counter" counts faster.
Make your ShootingTimer a float variable instead of int and so this instead:
ShootingTimer -= Time.deltaTime;
This will reduce ShootingTimer by 1.0 every second. So if you want one shot every second you have to set ShootingTimer to 1.0 if you want two shots in a second set it to 0.5
thanks for every one ...
I made the changes but it doesn't work my code is now :
var ShootingSpeed = 0.5; private var ShootingTimer = ShootingSpeed;
function AimAndShoot() {
var hit1 : RaycastHit;
var direction = transform.TransformDirection(Vector3.back);
if ( ShootingTimer > 0.0 )
ShootingTimer -= Time.deltaTime;
else
ShootingTimer = ShootingSpeed;
if ((ShootingTimer==ShootingSpeed)&&( Physics.Raycast(ShootPos, direction, hit1, 15) ) )
{
var newSight;
if(hit1.collider.gameObject.tag =="Players")
{
//print("Player got shooted");
//var newBlood = Instantiate(Blood,hit1.point, Quaternion.identity);
//newBlood.transform.rotation = Quaternion.FromToRotation(Vector3.down, hit1.normal);
}
else
if(hit1.collider.gameObject.tag =="Cars")
{
var newSpark = Instantiate(Spark,hit1.point, Quaternion.identity);
newSpark.transform.rotation = Quaternion.FromToRotation(Vector3.down, hit1.normal);
newSight = Instantiate(SightOfBullet,hit1.point, Quaternion.identity);
newSight.transform.rotation = Quaternion.FromToRotation(Vector3.down, hit1.normal);
newSight.parent = GameObject.Find("AllBullitSights").transform;
}
}
currentanimation = "shoot";
if ($$anonymous$$uzzle1.renderer.enabled)
{
$$anonymous$$uzzle1.renderer.enabled = false;
$$anonymous$$uzzle2.renderer.enabled = false;
$$anonymous$$uzzlelight.transform.GetComponent(Light).enabled = false;
}
else
{
$$anonymous$$uzzle1.renderer.enabled = true;
$$anonymous$$uzzle1.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
$$anonymous$$uzzle2.renderer.enabled = true;
$$anonymous$$uzzlelight.transform.GetComponent(Light).enabled = true;
audio.clip = SoilderSounds.Shoot;
audio.Play();
}
}
but still the same problem .... (the speed of shooting slower than first ,but still when I rotate the player, the speed will increase as you said "framerate is probably higher when the other model isn't rendered"
I tried to put the comment up in a code format but it's doesn't work , I put the code at pastebin site then copied to here but the same problem ×_× ( sorry )
What is the point of this:
if ( ShootingTimer > 0.0 )
ShootingTimer -= Time.deltaTime;
else
ShootingTimer = ShootingSpeed;
just use:
if (ShootingTimer > 0.0)&&( Physics.Raycast(ShootPos, direction, hit1, 15) ) )
finally I found the solution :
private var fireRate = 0.09;
private var nextFireTime = 0.0;
function AimAndShoot() {
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
}
thanks u all , without your help I couldn't fix it (^_^)
Your answer
Follow this Question
Related Questions
Where is the FPSPlayer Script? 0 Answers
Player moves faster when fps is higher? 1 Answer
How to run a script to only 1 player in a multiplayer fps 1 Answer
Question about FPS Player Script 1 Answer
Line Renderer Collision Detection. 2 Answers