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 Satamanster · Feb 15, 2013 at 01:37 PM · distancevectorshoot

Create a vector between me and my enemy.

Hello everyone, i'm working on a plane game and i want my enemy to attack me when is a certain distance from me, since i'm shooting bullets as well i can't use a sphere trigger collider around me because my bullets hit that collider so i was thinking using a vector traced from my enemy position to me and everytime it was shorter than a certain distance it would start shooting me. The question is, how do i create this vector keeping it updating?

I'm using this condition to shoot:

 var bulletPrefab: Transform;
 var bulletSpeed = 60;
 var nextShotTime : float = 0.0;
 var timeBetweenShots : float = 2.0;
 //var shootSound: AudioClip;
 
 function Update()
 {
     if (***********)) //this is where my vector condition would enter
     {
 //        Shoot();
         if (nextShotTime <= Time.time)
           {
               Shoot();
               nextShotTime = Time.time + timeBetweenShots;
         }
     }    
             
 }
 
 
 function Shoot()
 {
     var shoot = Instantiate(bulletPrefab,GameObject.Find("Spawn").transform.position,GameObject.Find("Spawn").transform.rotation);
             shoot.rigidbody.AddForce(GameObject.Find("Spawn").transform.forward * bulletSpeed);
 //    AudioSource.PlayClipAtPoint(shootSound, GameObject.Find("Spawn").transform.position);
 }



Can you guys help me out with this, doesn't seem very complicated but i really can't figure it out... Thanks everyone.

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

1 Reply

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

Answer by robertbu · Feb 15, 2013 at 02:33 PM

I assume this is the enemy script. You need the game object of the player (or the transform). Typically you would get this once at Start();

 var goPlayer : GameObject;
 
 function Start() {
      goPlayer = GameObject.Find("Player");
 //alternate goPlayer = GameObject.FindWithTag("Player");
 }

Then in update you can test the distance:

 if (transform.position - goPlayer.transform.distance).magnitude < someValue) {
    // Shooting stuff
 }

 
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 Satamanster · Feb 15, 2013 at 02:43 PM 0
Share

Perfect!!! Works like a charm... ;)

avatar image jogo13 · Feb 15, 2013 at 10:30 PM 0
Share

There's a built-in distance function here: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Distance.html

avatar image hoy_smallfry · Feb 15, 2013 at 10:37 PM 0
Share

Performance optimization - use:

if (transform.position - goPlayer.transform.distance).sqr$$anonymous$$agnitude < (someValue * someValue))

Getting the magnitude relies on finding the square root (see: Pathagorean Theorem), and square root computations are much more computationally expensive than multiplication. If you use a squared magnitude ins$$anonymous$$d, the square root computation never happens. However, you must square 'someValue' to adjust the equation.

avatar image Satamanster · Feb 15, 2013 at 11:58 PM 0
Share

i went with

 if (transform.position - goPlayer.transform.position).magnitude < someValue)

and it worked eally well XD

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why this script is not working? 1 Answer

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

enemy shoots with animation 1 Answer

Disable & Re-enable Script 1 Answer

Script Conversion JS to C# 3 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