Hello , i have a 2.5d platformer with a player and some boxes and i can jump around , crouch , move rigid bodys with the player . Now i want to add a gun in the project. I want to be able to shoot at mouse pointer , rotate the player in the direction that my mouse is on my screen . This is a 3d game with the camera moved to look like a 2d platformer . I`ve searched but i cannot find a good script to start with. Thank you in advance !
u need to addforce to a gameobject and make it appear from a single point
attach an empty gameobject from where u want the bullets to appear(shoot) to your desired gameobject (your gun in this case)
create a sphere make it look like a bullet adjust it on unity it is easy
create a prefab on a folder and drag this bullet to the thePrefab variable below
then attach this script to your fps game object
private GameObject thePrefab;
public float shootingforce=40;
if(Input.GetMouseButton(0)
{
instance = Instantiate(thePrefab,transform.position,Quaternion.identity)as GameObject;
instance.rigidbody.AddForce(transform.right*shootingforce);
Destroy (instance.gameObject,0.5f);
…
i have not tested but it must work if u follow the steps
It may help if you break this up into simple questions.
E.G
Q: How do I get the location of my mouse in the world?
A: Use a raycast from the mouse pointer into the world.
Q: How do I rotate my character to face the mouse pointer?
A: How is your character controlled? Mechanim? You could use something simple such as Transform.LookAt.
Q: How do I shoot?
A: Fire a raycast from the barrel of your gun and see what you hit or instantiate a projectile at the barrel and give it a velocity in the forward direction.
If you ask simple questions you will get a better response.
Karl