• 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 dalessan9 · May 18, 2017 at 12:36 AM · c#movementenemydirectionbullet

C# - 2d --I have two questions... both seem like they should be incredibly simple - script hive mind + MoveTo not follow

I'm trying to solve two problems I can't figure out.

First - with the following code I create a delay - so the enemy waits a tick after spawning before shooting. Problem 1 -- This delay works - but - every spawned instance w/ the script -- has the exact same delay -- and enemies spawned later don't have a delay. == how do I even...? I thought Start() was for that instance of the script? What should I use?

 void Start () {
         nextAttack = Time.deltaTime * (Time.time + attackSpeed + Random.Range(-0.5f,0.5f));
     }

Second, another problem I've been having is -- moving in the direction of a saved location -- rather than following the thing that it should have saved the location of. I figured the problem was in update -- so I've tried moving it to start -- the following is my latest attempt -- this i've no fcking clue what this is doing -- it actually moves -- but in a random direction -- w/ some bullets travelling at a different speed than others...Idk why -- The commented lines don't actually work -- other than Vector2.Left -- quite a few didn't actually move at all -- so needed to check it wasn't somewhere else causing the problem

 public float projectileSpeed;
 public bool hasPattern;
 public bool aimsAtPlayer;
 private Rigidbody2D myRigidBody;
 public GameObject myTarget;
 public Transform targetLocation;

 // Use this for initialization
 void Start () {
     myRigidBody = GetComponent<Rigidbody2D>();
     targetLocation = myTarget.transform;
     if (!aimsAtPlayer){
         myRigidBody.velocity = Vector2.left * projectileSpeed;
     }

     if (aimsAtPlayer)
     {    
         myRigidBody.velocity = Vector2.MoveTowards(-myRigidBody.gameObject.transform.position, targetLocation.position, projectileSpeed);
         //myRigidBody.gameObject.transform.LookAt(myTarget.transform);
         //myRigidBody.transform.position = Vector2.MoveTowards(myRigidBody.transform.position,myTarget.transform.position,projectileSpeed * Time.deltaTime);
         //myRigidBody.velocity = Vector2.left * projectileSpeed * Time.deltaTime;
         //myRigidBody.transform.position = Vector2.Lerp(myRigidBody.transform.position, myTarget.transform.position, projectileSpeed * Time.deltaTime);
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Yoshinator2 · May 18, 2017 at 01:01 AM

For your first problem, your explanation is kind of unclear, but if I'm right, the delay happens because every object spawned runs the script in their own way, thus giving them each a delay.

For the second problem, if myTarget gameobject moves but you only want it to move in the direction in which myTarget started as, just create a new script and put it on an empty game object, and have it store the original position of myTarget, and have the other scripts use it as a reference in the start function to set the target to that.

If you could clarify the first problem more I may be able to help. If these solved your problems, please accept my answer. If not, feel free to ask more questions!

Comment

People who like this

0 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 dalessan9 · May 18, 2017 at 11:25 AM 0
Share

1st problem -- I have that as part of the script attatched to enemy gameobjects -- so that after spawning -- they cannot immediately fire their weapon at the player. For the first group of enemies that gets spawned -- a delay does occur. The same amount of delay. If it randoms out to 4 seconds -- every enemy that was spawned will fire after that 4 second delay. -- For every enemy spawned after the initial group there is no delay. -- as in they start firing - immediately after they are spawned. -- This question is more -- are the enemies running seperate instances of this script connected -- or is the random.range not truly random/ not used properly -- and why don't enemies spawned in -- wave 2+ have a delay -- so they aren't shooting at you from off-screen - if - as my understanding permits - the moment after they spawn in - they should call their Start() function

2nd problem -- I don't want to save the the start position -- I'm making 2D shmup -- I want the enemy to fire at player current position -- with a bullet that doesn't track towards the player if the player decides to move out of the way. All of the methods I've come up with - 'home in' - partially because they happen in update -- but more-so because I have no idea how to set direction of movement properly. The question would benefit more from an answer that describes a movement method that lets you set angle prior to the moving starting towards that vector

avatar image Yoshinator2 dalessan9 · May 18, 2017 at 03:25 PM 0
Share

Each object runs a different instance of the script. So yes, every object you spawn will run their own start() function and produce a different random.range value. If you want only one global value for all of the enemies to wait for, which is a set value, create a new script called something like InfoHolder and attach it to an empty game object in the scene. Only have one of these. Have the InfoHolder run the random range, and set a public float to the value. Then have the enemies scripts use the InfoHolder value as a refence for the delay.

To get the character to aim at the player, just get the angle between the two, and set the rotation of the enemies gun to the same. Keep in mind you will have to tweak it for when the enemy is facing right or facing left. Here is an example:

     var angle : float = Mathf.Atan2(p2.y-p1.y, p2.x-p1.x)*180 / Mathf.PI;

p1 just means position 1 and p2 is just position 2. Add your own variables into that and it should work fine.

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

7 People are following this question.

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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Game 2x speed object pass through other 1 Answer

Unity2D, How do i check which way a sprite is facing? 1 Answer

Enemy shouldn't walk with the player 2 Answers

Create an array that chooses between four random numbers (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