- Home /
How can I shoot bullets that you can aim with the crosshair?
I just implemented a shooting mechanism into my gun where bullets are created and move out of the barrel of the gun forwards and since they come from the barrel, their direction doesn't match the crosshair in the middle of the screen. How can I make the bullets move so you can aim with the crosshair? here is my script:
/*
private void ShootGun() {
GameObject bullet = Instantiate(bulletModel, gunPos.position, gunPos.rotation);
bullet.AddComponent(typeof(Rigidbody));
Rigidbody rb = bullet.GetComponent<Rigidbody>();
rb.useGravity = false;
rb.velocity = camera.forward * speed;
}
*/
Answer by azerty0220pl · Apr 09, 2020 at 12:15 PM
Hello @romans8710 You can solve it using raycasting:
//Create a ray. Syntax: Physics.Raycast(startPosition, direction, hitInfo)
Physics.Raycast(yourCamera.transform.position, yourCamera.transform.forward, RaycastHit hitInfo);
//the code you wrote now
bullet.AddComponent(typeof(Rigidbody));
Rigidbody rb = bullet.GetComponent<Rigidbody>();
rb.useGravity = false;
//Rotate the bullet towards the point where the ray hits a collider
bullet.transform.LookAt(hitInfo.point);
//It is better to add a force, not to modify velocity.
rb.AddForce(bullet.transform.forward * speed, ForceMode.VelocityChange);
It can have a weird effect if you are close to the target, but should work. If you want to learn more about raycasting, there is a video on YouTube made by Unity about it.
What is the hit info? I am really new this especially raycasting.
'hit info' is the object that is returned of type Raycast from shooting a ray. You can query this object for information about for example where it hit, how long it is away etc. As you can see from @azerty0220pl answer he/she first creates a Raycast called 'hitInfo' in this line:
//Create a ray. Syntax: Physics.Raycast(startPosition, direction, hitInfo)
Physics.Raycast(yourCamera.transform.position, yourCamera.transform.forward, RaycastHit hitInfo);
And then refers to the point where it hit in this line:
//Rotate the bullet towards the point where the ray hits a collider
bullet.transform.LookAt(hitInfo.point);
for more information, I suggest using unity learn. In particular something like this.
So what would I have to do for it to not be an error? I made it a RaycastHit variable and its till come up as an error.
@Aduis This may not be relevant anymore to you but just so people here aren't as confused when they see this solution (which is a good one), the syntax for the Raycast would be like follows RaycastHit hitInfo; Physics.Raycast(yourCamera.transform.position, yourCamera.transform.forward, out hitInfo);
The error you're getting is because it needs to be an output of "RaycastHit" variable type. Hope this helps!
Your answer
Follow this Question
Related Questions
FPS recoil 1 Answer
How to add sound to gun shot script 4 Answers
GetButtonDown problem | C# 0 Answers
How would I add force/ burst fire to my gun? 2 Answers
Shooting script problem. 2 Answers