Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Whatee · Feb 24, 2010 at 06:40 PM · enemyrangetornado-twins

Script so that, my enemies only see my player at a certain range.

Hi everyone,

I've been following the Tornado Twins tutorials on youtube and i just reached a snag.

In my scene i have turrets, whose head portion rotates along the base in order to aim at my player character. At the moment the turrets will aim and fire at my player character no matter where he is in game.

What i would like is for the Turrets to be offline or not working until the player character or TARGET has come within a certain range.

Thx.

This is my Script so Far

-------------------------------------------------------------------------------------------

var LookAtTarget:Transform; var damp = 6.0; var bulletPreFab:Transform; var savedTime=0;

var attackRange = 30.0; var shootAngleDistance = 10.0; var target : GameObject;

function Update() { }

if(LookAtTarget)
{
    var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
//Dampin/Delay on speed at which it follows the target
    transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

var seconds : int = Time.time; var oddeven = (seconds % 2);

if(oddeven) {
Shoot(seconds); } }

//Turret Shoot function

function Shoot(seconds) { if(seconds!=savedTime) { var bullet = Instantiate(bulletPreFab, transform.Find("SpawnPoint").transform.position, Quaternion.identity); //Bullet speed bullet.gameObject.tag = "enemyProjectile"; bullet.rigidbody.AddForce(transform.forward * 800);

savedTime=seconds; } //Removed this peice of code, to make it more complex //transform.LookAt(LookAtTarget); //if an enemy as further than maxDistance from you, it cannot see you

}

Comment
Add comment · Show 1
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 burnumd · Feb 25, 2010 at 02:10 PM 1
Share

Could you fix the code segments of your question? It's difficult to read what's going on with the formatting the way it is.

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Shawn · Feb 24, 2010 at 06:47 PM

You would just want to do a distance check to the target... Something like Vector3.Distance(LookAtTarget.position, transform.position) should do the trick. This will return a float of the distance between the 2 points; compare that distance to your desired distance.

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
1

Answer by burnumd · Feb 24, 2010 at 08:34 PM

If your player has a rigidbody and is tagged "Player" (if you're using the default FPS Walker prefab, it already has these things), you can do the following:

Add a sphere collider to your turrets (make sure you "Add" and don't "Replace" any other colliders you have on the turret).

Make the sphere collider a trigger.

Make the radius equal to your desired detection distance.

Add the following to your turret script:

function OnTriggerEnter (other : Collider) { if (other.CompareTag("Player")) { // lookAtTarget corresponds to the LookAtTarget variable you defined. // In Unityscript, the preferred style is to start variables with a lowercase. lookAtTarget = true; } }

function OnTriggerExit (other : Collider) { if (other.CompareTag("Player")) { lookAtTarget = false; } }

Letting Unity's collision system handle distances is preferable to checking the distance every frame (see Unity's Optimization Docs).

Comment
Add comment · Show 3 · 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 Whatee · Feb 24, 2010 at 09:07 PM 0
Share

Where should i add this script?

avatar image burnumd · Feb 25, 2010 at 02:09 PM 0
Share

Place it anywhwere outside your Update function inside the turret script. These are both separate functions you can implement in any script and if a collider is attached to the same object, these functions are automatically called when a trigger event occurs (see the chart at the bottom of this page for the conditions: http://unity3d.com/support/documentation/Components/class-BoxCollider.html ).

avatar image bdover1284 · Oct 29, 2012 at 05:15 PM 0
Share

I was having trouble with the same script and used your script but get this: Assets/scripts/TurretControl.js(29,32): BCE0022: Cannot convert 'boolean' to 'UnityEngine.Transform'.

avatar image
-2

Answer by Whatee · Feb 24, 2010 at 07:08 PM

hehe, Thx for the help.

Just not exactly sure where i would put that in?

Comment
Add comment · Show 2 · 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 runevision ♦♦ · Feb 24, 2010 at 07:42 PM 0
Share

When replying to an answer, please use the "add comment" link rather than posting a new "answer" to the question.

avatar image Shawn · Feb 24, 2010 at 08:30 PM 0
Share

You can put it wherever works best for your script. $$anonymous$$aybe where you select the LookAtTarget just do a check before assigning that value.

avatar image
0

Answer by Grimlock257 · Nov 15, 2010 at 10:13 PM

I finished there tutorials too, here is my script with a few added bits, it looks at you but dost fire until your in range, you can mess around with these settings if you like, if you have not solved you problem then copy this, if your solved then OK then

//Movment and Target

var LookAtTarget:Transform; var damp = 1.7; var ememy : Transform;

//Shooting var bullitPrefab:Transform; var savedTime=0; var curShoot = true;

function Update () { if(LookAtTarget) {
if ( Vector3.Distance( ememy.position, transform.position ) < 18 ) //'18' is the distance the turret starts to look at you

var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);

      transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

      if(curShoot)
      {     
             var seconds : int = Time.time;
             var oddeven = (seconds % 2);

             if(oddeven)
             {     
                    Shoot(seconds);
             }
      } 

} }

function Shoot(seconds) {
if ( Vector3.Distance( ememy.position, transform.position ) < 15 ) //'15' is the distance the turret starts to shoot at you { if(seconds!=savedTime) {
var bullit = Instantiate(bullitPrefab ,transform.Find("SpawnPoint").transform.position , Quaternion.identity); bullit.gameObject.tag = "ememyProjectile"; bullit.rigidbody.AddForce(transform.forward 1600);
bullit.rigidbody.AddForce(transform.up
45); savedTime=seconds; }
} }

Hope this helps

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

2 People are following this question.

avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Turret Range Script 2 Answers

Clarification on how RangeAttribute works 1 Answer

MouseButtonDown on button (Far away) NEED: Range for button 2 Answers

Detect if player is in range? 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