The Y-Rotation of a object

Hello,

This is my script:

void Update () {
	
		if(Input.GetButton("Fire1") && Time.time > nextFire){
			
			nextFire = Time.time + fireRate;
			
			//Get the launch position
			
			bulletLaunchPosition = bulletLauncher.transform.position;	
			
			//Create the bullet at the launch position
			
			Instantiate(bullet, bulletLaunchPosition, bulletTransform.rotation);
			
			
		}
		
	}

And so now, when I shoot, there is a bullet prefab that will get instantiated when I press the Fire1 button, but when I shoot the Y rotation doesn’t change, so when I look up, it just shoots horizontaly over the terrain.

Can someone help me.

Your question is confusing. You write of "when I look up" which indicates you want to fire in the direction of the camera, but you use bulletLauncher in your code which indicates some sort of gun being aimed. For an accurate answer, we need to understand how you have things setup...where is the bullet fired from and how is it aimed.

So, the bullet is fired from a empty gameobject. and when I look with the fps controller standard asset, with a gun on the camera, and the object on the barrel of the gun. It doesn't go in the Y rotation. So like when I look 90 degrees in the Y rotation, the bullet goes 0 degrees in the Y rotation.

Does the gun tilt up? What you are doing here in this code is confusing. Typically you would have this script attached to the spawn point and do something like: var go = Instantiate(bullet, transform.position, position.roation); go.AddForce(go.transform.forward, 1000); Given the limited amount of code you've shown, I don't know how bulletTransform is set and what it represents.

1 Answer

1

If you want to shoot in the direction you’re looking, use the camera forward direction. From your code, it seems that the bullet accelerates itself in its own script, since there’s no code to move it. If the bullet goes in its forward direction, use the camera rotation:

Instantiate(bullet, bulletLaunchPosition, Camera.main.transform.rotation);

Thx alot, this works