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 Mightycam · Nov 20, 2014 at 04:20 AM · raycastaudiodistancefiring

Gun Turret Script. Help with distance C# Script

Hey guys I'm going to try and be as specific as can be as I'm completely new to C# Scripting. I'm trying to make a Gun Turret shoot at the play which is all sorted. But I'm having problems, I need to set 2 different distances one for the turret to start shooting and the other to start an alarm. Every time I set the distance for example the turret to shoot at the player 10 meters away from the turret. But it just shoots regardless if I set the distance. Here's the script below

using UnityEngine; using System.Collections;

public class Turret : MonoBehaviour {

 public Transform target;
 public float distance;
 public float alarmRange;
 public float firingRange;
 public TurretFiring turretFiring;


 void Update () 
 {
     transform.LookAt (target);
     distance = Vector3.Distance(transform.position, target.position);
     if(distance <= firingRange)
     if(distance <= alarmRange)
     {
         turretFiring.canFire = true;
         audio.Play();
     }
         else
         turretFiring.canFire = false;
         audio.Play();
     
 }

}

Comment
Add comment · Show 5
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 Mightycam · Nov 20, 2014 at 07:26 AM 0
Share

Sweet thank you for your time. Although, when set my distance to 10 for the turret and 15 for the audio. It's still shooting when I'm not in the distance.

avatar image Uldeim · Nov 20, 2014 at 07:31 AM 0
Share

The problem might be in the TurretFiring code then. Have you tried using Debug.Log to make sure all the booleans come out correctly?

avatar image Mightycam · Nov 20, 2014 at 07:46 AM 0
Share

No, I'm still learning about this and I'm trying my best to sort out as much as I can but struggling at the moment. Here's the turretFiring script. Even if you help me with whats wrong and I can try fix it myself that would be great :)

 using UnityEngine;
 using System.Collections;
 
 public class TurretFiring : $$anonymous$$onoBehaviour 
 {
 
     public Rigidbody projectile;
     public float projectileSpeed;
     public float coolDown;
     public float timer;
     public bool canFire = false;
     public GameObject particlePrefab;
 
     void Update()
     { 
             
             timer += Time.deltaTime;
             if(timer > coolDown)
                 {
                     Fire ();
                     timer = 0;
                 }
             }        
     
         void Fire()
         {
             GameObject particle;
             particle = Instantiate(particlePrefab, transform.position, transform.rotation)as GameObject;
             Destroy (particle,1.0f);
             Rigidbody clone;
             clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
             clone.velocity = transform.TransformDirection(Vector3.forward * projectileSpeed);
         
             //clone.velocity = transform.TransformDirection(Vector3.forward * 10)
         }
 
 }
avatar image Uldeim · Nov 20, 2014 at 07:48 AM 0
Share

Ah, yes, see how you're not actually checking canFire? Try adding it to the if timer > coolDown condition:

if (timer > coolDown && canFire)

avatar image Mightycam · Nov 20, 2014 at 09:17 AM 0
Share

PERFECT!!!

Thank you so much. It's been stressing me out.

1 Reply

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

Answer by Uldeim · Nov 20, 2014 at 04:36 AM

Your if statements are malformed. That's likely the main cause of your issues. This is the most basic of C-like syntax, so if you're having trouble, I suggest looking at basic tutorials for JS, C, C++, C#, or Java, all of which use very similar syntax for all basic operations (though obviously, C# would probably be better, since you're using it here).

The crux of the issue displayed is that the first if, distance less-than-or-equal to firingRange, has no brackets. That means that it affects only the next line. Unfortunately, the next line in this case is your other if. The second if does have brackets, so it includes everything within them.

The else statement suffers from a similar fate. With no brackets, it only applies to the first expression after it, setting canFire to false. Audio will always play.

So therefore, in English, what you are saying is:

If Distance is less-than-or-equal to FiringRange then, if Distance is ALSO less-than-or-equal to AlarmRange set canFire to true and play my audio. If Distance is not less-than-or-equal to AlarmRange but is still less-than-or-equal to firingRange, set canFire to false. Then, regardless, play my audio.

What you probably meant is:

  if(distance <= firingRange)
  {
      turretFiring.canFire = true;
      audio.Play();
  }
  else if(distance <= alarmRange)
  {
      turretFiring.canFire = false;
      audio.Play();
  }


There are more efficient ways to do that, but that should work fine.

Lastly, a minor quibble: After pasting code, select it all and click on the little 101010 button. It'll avoid parts of it being outside the Code Block, such as the class definition and closing bracket in your question.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Damage alterations at different range. 1 Answer

Can anyone help me modify this script? (C#) 2 Answers

How to read jump height of character? 2 Answers

How can i tell the distance from a raycast? 1 Answer

How can I draw a specific distance with Debug.Drawray 4 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