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 /
avatar image
0
Question by Conny14156 · Sep 28, 2011 at 08:11 PM · raycastgetcomponent

How do I get my Fireball toward my mouseposition?(some non working tries example in here :3)

Iam complety new to unity, and programming etc. so i would appreciate if anyone can help me :3

I been trying to get a prefab position, when my "charchter" (a cube at the momemnt) istantiate it. or somehow my stupid fireball(prefab) when created to go to my mouseposition somehow. oh iam trying to do a 3d game for my school project

the first alternative use these variables.

     public GameObject Target;
 public int MovementSpeed = 5;
 Vector3 TargetVector3;



     Target = GameObject.FindGameObjectWithTag("SpellsTarget");
     TargetVector3 = Target.transform.position;
     
     Debug.Log(Target);


alternative try 2


Target = GetComponent; TargetVector3 = Target.transform.position;


3rd try


 Direction = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
     if(Physics.Raycast(Direction,out hit,100))
     {
         TargetPosition = new Vector3(hit.point.x,0,hit.point.z);
         Debug.DrawLine(Direction.origin,hit.point);
     }

( from another script i try to acess the TargetPosition by doing

Controller_Movements RayHitPoint = GetComponent(); TargetPosition = new Vector3(RayHitPoint.TargetPosition.x, 0, RayHitPoint.TargetPosition.z);


another try is


TargetPosition = GameObject.FindGameObjectWithTag("SpellsTarget").transform.position; transform.position = new Vector3(TargetPosition.x,0,TargetPosition.z);

(and more) (cant bother to write down more just need ONE of these to work :( ) i just want to somehow to get my "fireball" toward my mouseposition. be it either raycast or toward a object spawning near my mouse. (i have a working code where i can spawn a empty object on my mouse position)

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 syclamoth · Sep 28, 2011 at 11:44 PM

Have an empty behind the camera which spawns fireballs. Then, use Physics.Raycast to find the point in your game that the mouse is over, and then use Vector3.Interpolate between the fireball spawner and the point on the ground! The cleanest way of doing that, would be to spawn the fireball, and then pass an init method which does something like this-

 public void Init(Vector3 newTarget)
 {
     target = newTarget;
     startPos = transform.position;
 }

then in your Update-

 void Update()
 {
     shotLocation += Time.deltaTime * shotSpeed;
     transform.position = Vector3.Lerp(startPos, target, shotLocation);
     if(shotLocation >= 1)
     {
         Explode();
     }
 }

When you spawn the fireball, do something like this-

 GameObject newFireBall = Instantiate(fireballPrefab, transform.position, Quaternion.identity) as GameObject;
 newFireBall.SendMessage("Init", targetPoint);
Comment
Add comment · Show 3 · 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 Conny14156 · Sep 29, 2011 at 07:20 AM 0
Share

but that only work for FPS right? my camera hover likes warcraft 3 or other strategy games, so wouldt the distance be wrong? (cant access my game project atm so i cant try :( )

avatar image syclamoth · Sep 29, 2011 at 03:03 PM 0
Share

Well, not exactly- the distance can be as high as you want, so long as the time remains the same! Ins$$anonymous$$d of instantiating the fireball behind the camera like I said, instantiate it from the character's transform.position. The rest of the code remains the same.

Assu$$anonymous$$g that you are looking down at a map, you can use

 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 RaycastHit hit;
 if (Physics.Raycast (ray, out hit)) {
     target = hit.point;
 }

Then do what I said above, but using that target point, and the caster's transform as the end and start points respectively!

avatar image Conny14156 · Oct 02, 2011 at 10:28 PM 0
Share

Yay its work, thank you :3

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Using raycast - Why can't I get a variable on the object I hit? 1 Answer

Raycast to Disable Script 0 Answers

Issue with Destroy() when identifying the gameObject with a RayCast 1 Answer

Why isn't this raycast working? 2 Answers

using Physics.Raycast to run functions on diffrent gameObjects? 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