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 LordJulian · Nov 14, 2011 at 07:53 AM · aiturret

Turret AI Script

Hello,

I've created a turret script out of several tutorials, an enemy AI turret that will fire at the player helicopter, but it doesn't seem to detect the target that it should fire at. The objectives of the script are as follows :

  • 3 dimensional rotation of the turret *Able to fire a projectile that can be dodged from the target

  • detection range that the turret "looks" at the target and fires when the target gets close *The projectile spawns from the projectile spawn point *The projectile flies depending on how fast the variable input is

This is the script:

var lookAtDistance = 15.0;

var distance;

//Target to be aimed at

var findTarget : Transform;

//Projectile/Ammo from Turret

var ammoPrefab : Transform;

//Tells Unity When to fire

private var fireCycle : float;

//Fire Delay

var fireDelay : float;

function update() {

distance = Vector3.Distance(findTarget.position, transform.position);

if(distance < lookAtDistance){

//Follows target

transform.LookAt(findTarget);

}

//Check if Turret can fire

if(Time.Time > fireCycle){

//Fire

shoot();

//Update Firing Time

fireCycle = Time.Time + fireDelay;

}

}

//Turret Fires

function shoot(){

var ammo = Instantiate(ammoPrefab.transform.Find("fireOutput"),transform.position,

Quaternion.identity);

ammo.rigidbody.AddForce(transform.forward * 500);

}

Any advice would be appreciated.

Comment
Add comment · Show 2
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 timsk · Nov 14, 2011 at 12:27 PM 1
Share

I'd love to help you, I like doing AI. However, there are a few problems with your question, firstly:

it doesn't seem to detect the target that I'm firing

I don't understand that statement, does it not detect the target, or does it not detect the firing? It's a confusing sentence.

Also, I'm not entirely sure what your problem is, you have given us a blob of code and told us what you want it to do. What problems are you having? What have you tried and why didn't it work?

Also:

This is the script that I'm currently following:

Is this a script from a tutorial your following, or a script you have written yourself and is not working as intended? Again, another confusing sentence.

avatar image LordJulian · Nov 15, 2011 at 07:00 AM 0
Share

Alright, sorry about the typo errors, I was in a hurry.

The turret(enemy) doesnt detect the helicopter(player) when I used this script.

I tried to follow this tutorial from the video here.

http://www.youtube.com/watch?v=tL-sn6r9YBw

It didn't work for me, then I tried to look at other answers from UnityAnswers and tried to fix it, but I'm stumped on why the script still doesnt work.

Hope that cleared up the misinformation.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by LordJulian · Nov 15, 2011 at 08:55 AM

I've solved the problem to my script, funny that all it takes is a few tweaks.

distance = Vector3.Distance(findTarget.**transform.**position, transform.position);

var ammo = Instantiate(ammoPrefab,transform.Find("fireOutput").transform.position , transform.rotation);

It finally works now, heh, thanks anyway for your effort to look into my Question, Timsk.

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

enemy AI script 3 Answers

Tank AI script that calculates distance of targets and firing angle 1 Answer

[Solved]How to destroy a turret in Tower Defense game 2 Answers

Trouble with turret script 1 Answer

Animay play in editor 0 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