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 Griffo · Aug 02, 2012 at 02:06 PM · ifvarelsevarable

Keeps returning True

Can someone tell me why playerBeingHit keeps returning True please ?

 #pragma strict
 
 var attackRange = 30.0; // Set the attack range
 var shootAngleDistance = 10.0; // Set the shoot angle distance
 var target : Transform; // Set the GameObject to shoot in the inspector window
 
 private var muzzelFlash : GameObject; // var for the GameObject with the MuzzelFlashSentryGun.js on
 private var globalVars : GameObject; // var for the GameObject with the GlobalVars.js on
 private var distanceToPlayer : int; // Distance from the enemy to the Player
 private var hit : RaycastHit;
 
 function Awake(){
 
 muzzelFlash = GameObject.Find("Muzzel Flash Sentry"); // Find the gameObject with the MuzzelFlashSentry attached to it
 globalVars = GameObject.Find("Global Vars"); // Find the gameObject with the GlobalVars attached to it
 
 }
 
 function Start(){
 
 }
 
 function Update(){
 
 muzzelFlash.GetComponent(MuzzelFlashSentryGun).audio.loop = false;
 
 distanceToPlayer = Vector3.Distance(target.position, transform.position); // Distance between the enemy and the Player
 
 if(distanceToPlayer < attackRange){
  shoot();
  }
 }
 
 function shoot(){

 globalVars.GetComponent(GlobalVars).playerBeingHit = false;

 // Rotate towards target 
  var targetPoint = target.position;
  var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
  transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
 
 // If we are almost rotated towards target - fire one clip of ammo
  var forward = transform.TransformDirection(Vector3.forward);
  var targetDir = target.position - transform.position;
  if (Vector3.Angle(forward, targetDir) < shootAngleDistance){
 
 // Send out a raycast from the GameObject forward to see if it hit anything
 if (Physics.Raycast (transform.position,transform.forward,hit, 10)) { 
  if (hit.collider.gameObject.tag == "Player"){ 
  blood();
        }
      }
    }
 }
 
 function blood(){
 
  if (hit.collider.gameObject.tag == "Player"){ 
  globalVars.GetComponent(GlobalVars).playerBeingHit = true; // Set playerBeingHit to true so the blood splatter will fade in/out
  muzzelFlash.GetComponent(MuzzelFlashSentryGun).muzzelFlash(); // Call muzzelFlash() on Muzzel Flash Senty GameObject
  }
 }
Comment
Add comment
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

2 Replies

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

Answer by Griffo · Aug 02, 2012 at 04:12 PM

Fixed the problem with the below script

 #pragma strict
 
 var trackDistance = 10; // Set te track distance
 var attackRange = 5.0; // Set the attack range
 var target : Transform; // Set the GameObject to shoot in the inspector window
 
 private var muzzelFlash : GameObject; // var for the GameObject with the MuzzelFlashSentryGun.js on
 private var globalVars : GameObject; // var for the GameObject with the GlobalVars.js on
 private var distanceToPlayer : int; // Distance from the enemy to the Player
 private var hit : RaycastHit;
 
 function Awake(){
 
 muzzelFlash = GameObject.Find("Muzzel Flash Sentry"); // Find the gameObject with the MuzzelFlashSentry attached to it
 globalVars = GameObject.Find("Global Vars"); // Find the gameObject with the GlobalVars attached to it
 
 }
 
 function Start(){
 
 }
 
 function Update(){
 
 distanceToPlayer = Vector3.Distance(target.position, transform.position); // Distance between the enemy and the Player
 
 if(distanceToPlayer < trackDistance){
  trackTarget();
    }
 }
 
 function trackTarget(){

 muzzelFlash.GetComponent(MuzzelFlashSentryGun).audio.loop = false;
 globalVars.GetComponent(GlobalVars).playerBeingHit = false;
 
 // Rotate towards target and track it 
  var targetPoint = target.position;
  var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
  transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
  
  if(distanceToPlayer < attackRange){
  shoot();
    }
 }
 
 
 function shoot(){
 
 // Rotate towards target 
  var targetPoint = target.position;
  var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
  transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
 
 // Send out a raycast from the GameObject forward to see if it hit anything
 if (Physics.Raycast (transform.position,transform.forward,hit, 10)) {
  if (hit.collider.gameObject.tag == "Player"){ 
  globalVars.GetComponent(GlobalVars).playerBeingHit = true; // Set playerBeingHit to true so the blood splatter GUI will fade in
  // on the Main Camera in FadeBloodInOut.js
  muzzelFlash.GetComponent(MuzzelFlashSentryGun).muzzelFlash(); // Call muzzelFlash() on Muzzel Flash Senty GameObject in MuzzelFlashSentryGun.js
     }
   }
 }
Comment
Add comment · 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
0

Answer by Jeffom · Aug 02, 2012 at 02:31 PM

Maybe because you only set the var true when the player is hit, if it only enters the blood() function on player hit, then the var playerBeingHit will never be false.

Comment
Add comment · Show 6 · 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 Griffo · Aug 02, 2012 at 02:40 PM 0
Share

Now you point it out I agree, so how can I make it false outside the update function ?

avatar image Jeffom · Aug 02, 2012 at 02:41 PM 0
Share

just set the property to false before the hit test ;)

avatar image Griffo · Aug 02, 2012 at 02:54 PM 0
Share

I've tried it in different places, when I set the property to false under function shoot() when I move sideways out of the sentry guns fire it sets to false but if I walk straight backwards out the range of the sentry gun it stays true .. ??

Edited script to show new problem.

avatar image Jeffom · Aug 02, 2012 at 03:02 PM 0
Share

playerHit = false

if( hit == "player" ) playerHit = true; blood();

and in the blood function set the playerHit = false again.

avatar image Jeffom · Aug 02, 2012 at 03:04 PM 1
Share

I think it would be better if you could redesign your code, making use of global flags to test hit usually is a bad idea @_@

just call something like player.takeHit() if the test is true.

Show more comments

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

7 People are following this question.

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

Related Questions

Keycode else 1 Answer

How stop a piece of code from running if the conditions are no more met. 1 Answer

BCE0044 Error in the Start function 1 Answer

Help playing the right animation 1 Answer

problem with instance 1 Answer


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