- Home /
Question by
d-ostapa · Sep 08, 2019 at 07:42 PM ·
scripting problembulletprojectileinstantiate prefab
Instantiate creating object far off the screen
Hi, I'm trying to make a projectile script for a project im working on, and whenever I run my script here and click on the area i want the projectile to go to, it creates the object not where i clicked and 100's of units away.
public class ATTACK : MonoBehaviour
{
public GameObject projectile;
public float bulletspeed;
Vector2 mousepos;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
mousepos = Input.mousePosition;
GameObject bullet = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
bullet.transform.Translate(mousepos * bulletspeed);
}
}
}
There is my script. Its a 2D mobile game if that means anything. If anyone knows anything about instantiate help would be nice cause i don't know anything about it
Comment
You need to use https://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html
to get camera's position and put positions debug so you can see where object is instantiating .