Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by unity_cbt15NQaalm1rQ · Apr 17, 2018 at 12:27 AM · 2d gameprojectiletargetcombat

make an enemy spell move towards player

Hi, I'm working on a 2D boss level with a simple set : my player on the left side of the screen and my boss on the right. The player is moving, the boss isn't. The boss is a lot taller than the player and shoots fireballs from its mouth (which is as for now just a gameobject with a particle system attached on it for testing purposes). Problem is they are going straight so they never hit my player.

alt text

I'm instantiating the attack with a script attached to my boss and setting the damage values with another script attached to my spell prefab. I tried to add a target gameobject and translate the position towards it but it just spawns right on my player.

Is there any way to add some lines on my script to get it working? It's my first game project so the code might be messy.

Thanks for your help!

 public class MonsterAttack : MonoBehaviour 
 {
     public GameObject spell;
 
     public GameObject spellSpawn;
     public GameObject spellSpawn2;
 
     public float spellForce;
 
     private Animator anim;
 
     private void Start()
     {
         anim = GetComponentInChildren<Animator>();
     }
 
     void Update ()
     {
         if(Input.GetButtonDown("Fire2") && anim.GetBool("isSitting") == false)
         {
             GameObject newSpell = Instantiate(spell, spellSpawn.transform.position, spellSpawn.transform.rotation);
             newSpell.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(-spellForce, 0f)); 
 
             anim.SetBool("isYelling", true);
         }
         else if(!Input.GetButtonDown("Fire2") && anim.GetBool("isSitting") == false)
         {
             anim.SetBool("isYelling", false);
         }
 
         if(Input.GetButtonDown("Fire2") && anim.GetBool("isSitting") == true)
         {
             GameObject newSpell = Instantiate(spell, spellSpawn2.transform.position, spellSpawn2.transform.rotation);
             newSpell.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(-spellForce, 0f));
 
             anim.SetBool("isYellingSit", true);
         }
         else if (!Input.GetButtonDown("Fire2") && anim.GetBool("isSitting") == true)
         {
             anim.SetBool("isYellingSit", false);
         }
 
 
     }
 }


sketch.jpg (108.7 kB)
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

Answer by tormentoarmagedoom · Apr 17, 2018 at 08:31 AM

Good day.

You can calculate the direction vector boss-player and then add force in that direction.

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 unity_cbt15NQaalm1rQ · Apr 18, 2018 at 11:29 PM 0
Share

Thank you! Took me a while to figure that one out but you pointed me in the right direction :)

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

144 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Target lead (aim ahead) problem 1 Answer

Final fantasy style combat Scene switch question 0 Answers

Rotate projectile sprite in the direction it is flying (like an arrow) 2D. 1 Answer

Calculate initial velocity given distance, gravity and angle for a projectile to hit the target at uneven ground 1 Answer

Firing a projectile change the direction of the other projectiles. 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