- Home /
Question by
Cornhelius · Jul 05, 2019 at 01:52 PM ·
2dmouseshooting
Shooting at mouse position in 2d from a Child Object
What I'm trying to do is shoot a projectile at the mouse position from an object that's parented to a moving platform. The problem I'm having is that the projectile always moves in the same direction regardless of the position of the mouse. (it moves left and down diagonally)
I have the following function in the object that shoots:
private void ShootProjectile()
{
shotCooldown = maxShotCooldown;
GameObject newProjectile = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
Vector2 direction = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position).normalized;
Debug.Log(direction);
newProjectile.GetComponent<AmmoController>().projectileVelocity = direction * (projectileForce + projectileChargeForce);
}
And this other line on the projectile script to move in said direction:
void FixedUpdate()
{
transform.Rotate(Vector3.forward * rotationDirection * rotationSpeed);
transform.position += projectileVelocity * Time.deltaTime;
}
When I do a trace to check the direction vector it always returns the same values regardless of mouse position. I'm at a loss since I copied those lines from a successful answer. is the problem the fact that the shooter is parented to a moving platform?(edit: no, removed the object from the parent and the behavior is the same) Any thoughts?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612204408im_/https://answers.unity.com/themes/thub/images/avi.jpg)