• 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
Question by squall_789 · Sep 02, 2010 at 02:47 PM · movementenemyfire

Force enemy to fire at a specific point, == appears not to work.

Hey guys.

. What I'm attempting to do (and having little success with) is to sort the script so that when the enemy is at for example equal to a certain point i.e: -3 in the .y axis a bullet is fired.

If anyone knows the best way of doing t$$anonymous$$s then I would greatly appreciate it, I'm loving unity and I'm learning quite quick, however in t$$anonymous$$s case I'm confused as to why if position == (insert number here) doesn't appear to work, clearly the enemy has to cross y.3 in order to reach .y 6 for example.

Right now the script is written so that if the enemy is below a certain point then he will stop, and if $$anonymous$$s STOP position is equal to what I want then he will fire. I'm assuming that if position.y == doesn't work because the enemy is moving too fast, however I would like to get around t$$anonymous$$s problem without slowing the enemy movement down.

Currently My enemy script is as follows

using UnityEngine; using System.Collections;

public class Enemy_Script : MonoBehaviour { public float MinSpeed; public float MaxSpeed;

 public float currentSpeed;
 private float x, y, z;
 public GameObject Projectile;
 float firingRate = 2f; //delay between shots, in seconds
 float lastFired = -100f; //absolute time of last fired shot

 // Use t$$anonymous$$s for initialization
 void Start()
 {
     //currentSpeed = Random.Range(MinSpeed, MaxSpeed);
     currentSpeed = 4;
     x = Random.Range(2f, 2f);
     y = 7.0f;
     z = 0.0f;

     transform.position = new Vector3 (x, y, z);
 }

 // Update is called once per frame
 void Update()
 {
     float amtToMove = currentSpeed * Time.deltaTime;
     transform.Translate(Vector3.down * amtToMove);

     if (Time.time < lastFired + firingRate)
     {
         return;
     }


     if (transform.position.y <= -6.0)

     {
         currentSpeed = 4;
         // currentSpeed = Random.Range(MinSpeed, MaxSpeed);
         transform.position = new Vector3(x, y, z);
     }
     lastFired = Time.time;
     //if (transform.position.y <= 4f)
     if (transform.position.y <= 2f)
         //currentSpeed = Random.Range(0, 0);
     {
         Vector3 rightposition = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
         Instantiate(Projectile, rightposition, Quaternion.Euler(0, 0, 90));


     }

     }

}

Thanks guys, I'm a little unsure how to stick a reward on my post, I cant see the option anywhere.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by spinaljack · Sep 02, 2010 at 03:09 PM

The reason why == doesn't work on position is due to float inaccuracy.

The method to get around t$$anonymous$$s is to use Mathf.Approximately or test for a range of values e.g.

if(pos >= 3 && pos <= 4){
  // do somet$$anonymous$$ng
}

Just make sure that the movement speed of the object is smaller than the range you're testing for or else it's possible that the object "leaps over" the detection zone.

P.S. you can only post bounties for questions after you've gained a certain number of points. However, you can give points to people just by up voting and marking answers as correct with the green tick ;) ;)

Comment
squall_789

People who like this

1 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 squall_789 · Sep 02, 2010 at 03:39 PM 0
Share

I'm currently not home, but once I get back I'll give this a shot. I was expecting it to be as I said, that I'm moving at a speed where when the frame updates I'm not on 3.0 exactly, I'm actually on 3.0012 or something similar. The answer was useful though. now I just need to go onto how to set up a delay between the shooting and the enemy moving across the screen again.

avatar image

Answer by burnumd · Sep 02, 2010 at 03:10 PM

You're right when you say that the object must cross the plane running through 0,3,0. Unity (and computer systems in general) operate in discrete time steps. So at one point it may be at 2.9 and in the next sample, it will be at 3.1. These distinctions are difficult to detect with the human eye, but they happen nonetheless. If it's very important that the enemy start firing as close to y=3 as possible, I'd set up a trigger collider at y=3 and instead of checking the object's position in update, have the trigger send a message to the object to start firing.

Comment
squall_789

People who like this

1 Show 5 · 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 burnumd · Sep 02, 2010 at 03:13 PM 0
Share

With colliders, of course, you still have to make sure that the collision boundaries of the object are large enough and the movement slow enough that the object doesn't skip over the collider entirely.

avatar image squall_789 · Sep 02, 2010 at 03:36 PM 0
Share

I did think of the collider route actually. However seeing as this is just the 1st enemy and I plan to have many other enemies throughout the stage doing identical things just from differing points across the screen, it seems that colliders are not the best answer in this case.

avatar image burnumd · Sep 02, 2010 at 05:48 PM 0
Share

Do you mean they'd be firing at conditions other than y=3? If so then, yeah, colliders may not be the way to go unless you just had a proximity trigger around your target (as an added bonus with this approach, you could have multiple targets as well).

avatar image squall_789 · Sep 02, 2010 at 11:22 PM 0
Share

Yes they will be appearing all over the screen at different y axis and differing x axis.

avatar image burnumd · Sep 03, 2010 at 02:05 PM 0
Share

So are you just looking for proximity to their target?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

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

How to make basic AI in a 2d game? 4 Answers

Enemy AI With changing Player 0 Answers

Play animation for horizontal and vertical movement? 1 Answer

Bullet fired at enemy only applying damage when the enemy moves 1 Answer


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