How can i Instantiate my projectile?
Hello folks! So I have this problem where I am blocked with a code, the code in question allows my character to shoot a fireball at the place where I click with my mouse, but, instead of doing that, the fireball goes to the place where the camera is, (the camera is not moving, only the character), I thinks it is a problem with Instantiate and the mousePos, any of you know how I can resolve this issue?
here is the code:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
GameObject sort = Instantiate(projectile, transform.position,
Quaternion.identity);
Vector2 mousePos =
Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 myPos = transform.position;
Vector2 direction = (mousePos - myPos).normalized;
sort.GetComponent<Rigidbody2D>().velocity = direction *
vittProjectile;
}
}
To maje this code I've watch this tutoriel on how to make a rougue like: https://www.youtube.com/watch?v=Ktcgz1_0JMI&t=1051s
Hope someone will be able to help me ^^
Hello
if "the fireball goes to the place where the camera is" is because you are misscalculating the vector.
Yeah but it dont really know how to correct that, I'm a total newbie in Unity so I'm not sure at all
Answer by tormentoarmagedoom · Mar 05, 2020 at 02:11 PM
Thats not Unity, thats math.
I dont know where you instantiate the object or where is the destination. but The direction of the projectile vector must be (Destination position - Origin position) All in world coordinats (not local)
If its not doing what you expect, check where your suposed "origin and destination" really are in the scene.
I 've tried to coorect my code and came to this one:
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
GameObject sort = Instantiate(projectile, transform.position, Quaternion.identity);
var mousePos = Input.mousePosition;
mousePos.z = 5;
var myPos = transform.localPosition;
var direction = (mousePos - myPos).normalized;
sort.GetComponent<Rigidbody2D>().velocity = direction * vittProjectile;
}
}
So now the fireball are shooting, but not where I want them to, thet always go in the upper right corner of the screen
Hello again.
Come on! aboard your problem step by step. If you are not able to do this, will ahve hard time program$$anonymous$$g. First, i told you that you need to use WORLD coordinates, not Local (if dont know what this means, you must go learn about this,before co$$anonymous$$g ask again!!!)
Lets see your Direction vector, you say its mousepos - mypos which seems correct. So now lets see what this 2 positions are:
$$anonymous$$ousePos i understand is the destination, but Input.mousePosition; is the position of the mouse in the screen, not in the wolrd coordinates, Investigate how to converts mouse position to World coordinates.
myPos is suposed to be the origin, but you are using transform.localPosition wth is the relative position og the object containing ths screipt in reference with its parent, not the world posiotion. Investigate and solve this before come here again!
Good luck!
Hi! Well thanks a lot for your help, I have found a solution and now my code works more properly!!
Here the code I made
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
GameObject sort = Instantiate(projectile, transform.position, Quaternion.identity);
var mousePos = Input.mousePosition;
mousePos.z = 10;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
var myPos = transform.TransformPoint(Vector3.right);
var direction = (mousePos - myPos).normalized;
sort.GetComponent<Rigidbody2D>().velocity = direction * vittProjectile;
}
}
Now the isues is that the fire ball is not 100% accurate and dont go excatly where I clic (I will say that tge accurency is arround 97%) and it slow down when i clic near my character But I dont know what cause this despite having done some reaserch. I hope that is the last time i will bother you with my code, but i thinks you have already helped me a lot and i thank you for that ^^