- Home /
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
}
}
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
}
}
}
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.
Now you point it out I agree, so how can I make it false outside the update function ?
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.
playerHit = false
if( hit == "player" ) playerHit = true; blood();
and in the blood function set the playerHit = false again.
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.
Your answer
Follow this Question
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