- Home /
shooting projectile on mouse position on 2d multiplayer
Hello,
I am trying to shoot a projectile in a 2D game through the mouse click position.
There are several code snippets similar to this one I am using:
if (Input.GetMouseButtonDown(0))
{
Vector3 worldMousePos = Input.mousePosition;
Vector2 direction = (Vector2)((worldMousePos - transform.position));
direction.Normalize();
// Creates the bullet locally
GameObject bullet = (GameObject)Instantiate(
wpn.projectile,
transform.position + (Vector3)(direction * 0.5f),
Quaternion.identity);
// Adds velocity to the bullet
bullet.GetComponent<Rigidbody2D>().velocity = direction * wpn.projectileSpeed;
}
}
The issue I am having as you can see is right here Vector3 worldMousePos = Input.mousePosition;
It should be Camera.main(Input.mousePosition)
but the issue I am having is a multiplayer 2D game (I am using photon) and I dont have a Camera.main.
I have a player with a Camera object and this is a weapon manager belonging to the player.
I tried with transform.parent.camera (to get player camera) but itdidnt work.
Answer by tormentoarmagedoom · Apr 04, 2018 at 07:00 PM
Good day.
Camera.main in this context is a Camera component inside a Object, so you need to refear the camera component inside your camera object:
transform.parent.camera.GetComponent<Camera>();
If helped, upvote/accept :D
Bye!
Hello!
Thanks for answering!
The parent componet has this property:
public GameObject plCam;
Which is a camera object as you can see
But I cannot acces from the child, it says transform doesnt have definition for plCam