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 ExyloN · Jul 10, 2012 at 11:11 PM · aigameturrettower

Turret AI doesnt hit enemy

Hey guys, im currently working on a MOBA game like League of Legends but the AI Turrets are giving me a hard time. So far the turret detects the enemy box, creates a muzzle effect when a ball is fired but the ball doesnt hit the box, it goes way on top of the target. Here's a screenshot: ![Turret][1] [1]: /storage/temp/1793-turretai.jpg


Firebal Code:

 #pragma strict
 var mySpeed:float = 10;
 var myRange:float = 10;
 
 private var myDist:float;
 
 function Update () 
 {
  transform.Translate(Vector3.forward * Time.deltaTime * mySpeed);
  myDist += Time.deltaTime * mySpeed;
  if(myDist >= myRange)
  Destroy(this.gameObject);
 }


Turret Code:

 #pragma strict
 
 var myProjectile : GameObject;
 var reloadTime : float = 1f;
 var turnSpeed : float = 5f;
 var firePauseTime : float = .25f;
 var muzzleEffect : GameObject;
 var errorAmount : float = .001;
 var myTarget : Transform;
 var muzzlePositions : Transform[];
 var turretBall : Transform;
 
 private var nextFireTime : float;
 private var nextMoveTime : float;
 private var desiredRotation : Quaternion;
 private var aimError : float;
 
 function Start () {
 
 }
 
 function Update () 
 {
  if(myTarget)
  {
  if(Time.time >= nextMoveTime)
  {
  //Continuar a obter a posiçao do inimigo
  CalculateAimPosition(myTarget.position);
  turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * turnSpeed);
  }
  
  if(Time.time >= nextFireTime)
  {
  FireProjectile();
  }
  }
 }
 
 // Quando um inimigo entra no range da tower
 
 function OnTriggerEnter(other : Collider)
 {
  if(other.gameObject.tag == "Enemy")
  {
  nextFireTime = Time.time+(reloadTime*.5);
  myTarget = other.gameObject.transform;
  }
 }
 
 // Quando um inimigo sai do range da tower
 
 function OnTriggerExit(other : Collider)
 {
  if(other.gameObject.transform == myTarget)
  {
  myTarget = null;
  }
 }
 
 function CalculateAimPosition(targetPos : Vector3)
 {
  var aimPoint = Vector3(targetPos.x + aimError, targetPos.y + aimError,targetPos.z + aimError);
  //Rotaçao da tower
  desiredRotation = Quaternion.LookRotation(aimPoint);
 }
 
 function CalculateAimError()
 {
  aimError = Random.Range(-errorAmount, errorAmount);
 }
 
 function FireProjectile()
 {
  nextFireTime = Time.time+reloadTime;
  nextMoveTime = Time.time+firePauseTime;
  CalculateAimError();
  
  for(theMuzzlePos in muzzlePositions)
  {
  Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
  Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
  }
 }

Any idea of what may be causing this?

P.S: Dont worry with the comments that are written in other language

Cumpz, ExyloN

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
1

Answer by DisruptionGames · Jul 11, 2012 at 08:26 AM

A personal Preference is to use: Physics.Raycast() paired with transform.SendMessage(); It does come off a bit easier since it doesn't require any large amount of Collider calculation and can be manipulated easier.

If you decide not to go with Raycasting the screen shot tends to give off the idea that its not even aiming correctly at the object. I find (Almost always) that Transform.LookAt(); and Quaternion.LookRotation() are similar but different. If you don't know already you can call Quaternion.LookRotation() with one simple calculation needed: transform.position - target.position The Documentation actually shows a great example of this: http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.LookRotation.html So all of this would look at bit like this:

function FireAt(target: Transform){ var relativePosition = target.position - transform.position; var exactRotation = Quaternion.LookRotation(relativePosition); transform.rotation = exactRotation + errorRotation; }

The reason that all of this works is because its calculating the position between the points for a more exact rotation to look at.

Also! For Screen-shots I prefer to use a program called Puush. It sounds weird but its really awesome! You can check it out if you like, it posts instantly to the internet what you decide to screenshot: http://puush.me/

Comment
Add comment · Show 4 · 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 ExyloN · Jul 11, 2012 at 10:10 AM 0
Share

where am i suppose to put that code?

avatar image DisruptionGames · Jul 11, 2012 at 10:12 AM 0
Share

That is the base for Ai$$anonymous$$g so its just what you add as a different function so its a simple copy and paste it. If you want me to help a bit more add me on skype or email me: DevonJavaScript @ Skype DevonJavaScript@Gmail.com

avatar image whydoidoit · Jul 11, 2012 at 10:18 AM 0
Share

@DisruptionGames - it would be great if you could keep the conversation on UA so that future searchers can see how the complete solution was arrived at.

avatar image ExyloN · Jul 11, 2012 at 10:47 AM 0
Share

we're are currently talking in skype but ill write the code when done

avatar image
0

Answer by gooongalooo · Jul 11, 2012 at 12:25 AM

...uhmmm...shouldn´t the collider be trigger? what about your tags? everything tagged correctly? is a script attached to your projectile that contains a OnTriggerEnter to check when the projectile hits the box?

Comment
Add comment · Show 1 · 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 ExyloN · Jul 11, 2012 at 08:05 AM 0
Share

the collider is trigger. all tags are correct, there is no OnTriggerEnter on the projectile but i dont think that the problem is in there because it doesnt even hit the box...

here's a screenshot: http://img827.imageshack.us/img827/7784/turretai2.jpg

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

Placing objects in TD Game 1 Answer

trouble with my lookAt script 0 Answers

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

Trouble with turret script 1 Answer

Agents in a distributed game 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