Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fadi-l · Jun 29, 2012 at 02:30 PM · playerfps

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();  
     } 
 
 } 
Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jun 29, 2012 at 02:42 PM 1
Share

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.

avatar image DaveA · Jun 29, 2012 at 05:48 PM 0
Share
  1. Please edit your question, paste your code and FOR$$anonymous$$AT it there, then clean up this comment.

  2. How are these called? From Update? I don't see where you are looking for any delay. How is ShootingTimer set, and reset?

avatar image fadi-l · Jun 29, 2012 at 06:06 PM 0
Share

sorry about that comment....

this link of the solider script http://www.mediafire.com/?b5qe0uzy3bq6nbb

avatar image fadi-l · Jun 30, 2012 at 08:16 AM 0
Share

this video about the problem : http://www.mediafire.com/?83n6p44uanqb4dv

avatar image FLASHDENMARK · Jun 30, 2012 at 09:34 AM 1
Share

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

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fadi-l · Jun 30, 2012 at 01:57 PM 0
Share

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"

avatar image fadi-l · Jun 30, 2012 at 03:31 PM 0
Share

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 )

avatar image fafase · Jul 01, 2012 at 08:54 AM 0
Share

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) ) )
avatar image fadi-l · Jul 01, 2012 at 09:58 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges