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 PaxForce · Jun 02, 2014 at 06:57 AM · castingspell

best way to cast fireball

this may be a dumb question, but since I'm only a beginner, I'll take my chances and I'll just ask:

How do you cast a spell (eg fireball)? I created a simple sphere (where the fireball will be "born") and made it a child of the magic staff my character is holding. I will later make the sphere invisible. I created a simple cube (where the fireball should hit) and made it a child of the enemy's torso. I will later make that cube invisible too. Now I would like the fireball to fly from the sphere to the cube after it's instantiated. I would guess that the best way is to go through Translate method. Or is there a better, more resource efficient way. Anybody...?

Comment
Add comment · Show 1
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 wijesijp · Jun 02, 2014 at 07:58 AM 0
Share

Your method is good

2 Replies

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

Answer by Andres-Fernandez · Jun 02, 2014 at 08:10 AM

As you already know the initial and final position (your staff ball and the cube), you only need to make your fireball translate from the initial to the final position. I suggest using a coroutine and Vector3.lerp to create the translation of the fireball over time. Check the manual on corountines, startcoroutine, and Vector3.Lerp.

Comment
Add comment · Show 10 · 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 Andres-Fernandez · Jun 02, 2014 at 08:50 AM 0
Share

As @trumpfan says, it depends on the mechanics. You could also get the direction from the initial and final positions and add a force to the fireball, or you could make the ball keep track of its target and follow it...

avatar image PaxForce · Jun 02, 2014 at 09:42 AM 0
Share

I don't know what you mean by "keep track of its target and follow it". could you code it out for me please? of course just in case it's only a few lines, I really don't want to bother you :)

avatar image Andres-Fernandez · Jun 02, 2014 at 10:55 AM 0
Share

It means that it follows the target like the ones in Baldurs Gate. You could move, but the projectile changed its direction to aim at you (cursed basilisk spits).

Depending on what you want, code will be different.

avatar image Andres-Fernandez · Jun 02, 2014 at 11:01 AM 0
Share

If you just want an object to go from point A to point B use a coroutine like this one:

 IEnumerator ThrowFireball(Vector3 initialPosition, Vector3 finalPosition, float speed) {
         // First get the time of the translation based on the distance between the two points:
         float timeToReachTargetPosition = Vector3.Distance(initialPosition, finalPosition) / speed;
 
         // Then start the translation:
         float time = 0.0f;
         while (time < 1) {
             time += Time.deltaTime / timeToReachTargetPosition;    // Get the current time gap
             transform.position = Vector3.Lerp(initialPosition, finalPosition, time);    // Set the position accordingly
             yield return null;
         }
         // Once time equals 1 the ball has reached the final position.
         // $$anonymous$$ake your target explode or substract some HP or whatever...
     }

This one doesn't follow the target if the target moves away from it. if you want it to follow the target (in case it flees back),you have to update the final position before the lerp with something like:

 finalPosition = target.transform.position;

Although that will require to recalculate the time too.

avatar image PaxForce · Jun 12, 2014 at 09:05 AM 1
Share

awesome! thank you.

Show more comments
avatar image
0

Answer by trumpfan · Jun 02, 2014 at 08:41 AM

It depends on the specific mechanics of the fireball, is this a set-piece that will always spawn and impact the same place like a scene in a movie?

Can the fireball miss?

The lerp method suggested by Andres would work well for an RTS-like projectile, or a set-piece.

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 PaxForce · Jun 02, 2014 at 08:48 AM 0
Share

no, it's not gonna be an RTS game. I would like it to be a super cool RPG game one day :) so the answer is yes, the fireball ought to miss time to time. what would you suggest?

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

24 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

Related Questions

Active spell casting 0 Answers

Problem with type casting from C# to JS 3 Answers

Dynamic Casting during runtime 1 Answer

How do I cast an IntPtr to another class? 0 Answers

Converting to ColliderCast - what's included in the PhysicsWorld and how do I include my own meshes? 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