- Home /
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)
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);
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 :( )
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!
Your answer
Follow this Question
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