how to make bullet go straight to middle of the screen
Hello, i know this question has been asked before, but non of those answers solved my problem, so i decided to ask myself.
i want the bullet to go straight to middle of the screen where my crosshair is. i set the crosshair to middle of the screen using Screen.width / 2
and Screen.height / 2
and i wrote this line of code to instantiate the bullet when firing
var bullet = Instantiate(_bulletPrefab, _bulletEmitter.transform.position, _bulletEmitter.transform.rotation);
_bulletPrefab is a bullet prefab _bulletEmitter is a place holder, where the bullet should be created at, right in front of the gun
and i use this line of code to move the bullet
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.right * 10
the _bulletEmitter might be a problem, maybe it's not pointing to middle of the screen but how do i know?? it's not just about human eyes, from what i see 1 pixel can make a difference to where the bullet is going, and to my eyes the emitter is pointing to middle of the screen.
what seems to be the problem here ?
can i tell the bullet to go to Screen.width and height / 2 no matter where the emitter is pointing at in my c# script ?
please tell me your ideas and tips thank you so much for reading my question
Answer by Hellium · Dec 21, 2018 at 07:02 AM
// Drag & drop the main camera in the inspector
public Camera Camera;
private void Shoot()
{
// Create a ray from the camera going through the middle of your screen
Ray ray = Camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
RaycastHit hit ;
// Check whether your are pointing to something so as to adjust the direction
Vector3 targetPoint ;
if (Physics.Raycast(ray, out hit))
targetPoint = hit.point;
else
targetPoint = ray.GetPoint( 1000 ) ; // You may need to change this value according to your needs
// Create the bullet and give it a velocity according to the target point computed before
var bullet = Instantiate(_bulletPrefab, _bulletEmitter.transform.position, _bulletEmitter.transform.rotation);
bullet.GetComponent<Rigidbody>().velocity = ( targetPoint - _bulletEmitter.transform.position ).normalized * 10
}
hello, thanks for the answer. what is that out hit
part ?
The out keyword gives the possibility to the Raycast
function to store information about the "collision" into the variable hit
(which I forgot to declare, I will fix the code)
Answer by toddisarockstar · Dec 21, 2018 at 07:06 AM
First of all never use devision unless you have to when programing. depending on the CPU its estimated that multiplication is 4 - 10 times faster than devision depending on the CPU. Proficiency is a good habit in game development so it should be Screenwidth * .5f instead of dividing by two.
anyways, screen position and world position are two entirely different things so you need special conversions. you can do a raycast into the scene with a screen position. here is a raycast from the mouse position but you could replace the mouse position with a screen coordinate:
there is also this option: link text
Answer by army666 · Dec 22, 2018 at 02:46 PM
i finally solved my problem, so i write what i did to fix it, in order to help other people who may have same issue.
the main problem was because my crosshair wasn't in exact middle of the screen, after 2 days of trying and trying i decided to take an screen shot and check to see which one is not centered crosshair or the bullet, and yes it was crosshair.
i worked on my crosshair code and image file and found out that when i use GUI.DrawTexture(new Rect(Screen.width * 0.5f, Screen.height * 0.5f , 15f, 15f, _crossHair);
unity puts the crosshair image in exact center of the screen but in fact, the left edge and upper edge of the image will be in center of the screen, but what i wanted was to put the center of the crosshair in center of the screen. so i changed my code to:
[SerializeField] private Texture2D _crossHair;
[SerializeField] private float _crossHairWidth;
[SerializeField] private float _crossHairHeight;
private void OnGUI() {
GUI.DrawTexture(new Rect((Screen.width * 0.5f) - (_crossHairWidth * 0.5f)
,(Screen.height * 0.5f) - (_crossHairHeight * 0.5f)
, _crossHairWidth, _crossHairHeight), _crossHair);
}
i hope it makes sense and helps someone.
Your answer
Follow this Question
Related Questions
How do I have my character aim where my mouse cursor is? 2 Answers
I get three errors, no idea why.. 1 Answer
How do i make my gun have a fire rate 0 Answers
Change starting scene based on file 0 Answers
Ammo changing between guns 0 Answers