I want to make an instantiation of bullet which direction is depend on mouse-y, so the y-direction of bullet can be changed according to the mouse-y. Can anyone help to modify my current code? i think the problem is in the transform.rotation. Thanks^^
var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation);
//set direction of bullet to move forward through z axis of world coordinate
instantiatedProjectile.velocity = transform.TransformDirection(Vector3(0, 0, speed));
As far as I see it, you're instantiating the bullet from your player gameObject. I assume you're making some kind of First Person Shooter where you turn your character with the mouse. Therefore your characters rotation should be appropriate for the bullet and you needn't rotate the projectlile after instantiation. However to answer your question:
If you have the rotation you would like the bullet to have, then you can simply use
var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, desiredrotation);
If you want to rotate the object after instantiation you can add this line of code
If you want to constantly change the rotation of your bullet, you need a seperate script. This script should be added to the projectile prefab. It may look like this:
var turnspeed : float;
function Update () {
transform.Rotate(0,Input.GetAxis("Mouse X")*Time.deltaTime*turnspeed,0);
}
var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, desiredrotation); in this case, i want to make the desiredrotation according to the mouse-Y. So, the crosshair can be up and down, and if it's up/down, then the bullet will be launched to that crosshair's position. How do i do that? Anyway, thanks for replying. it's very helpful.thanks.
I don't quite understand what you want to do. Do you have a problem in displaying a crosshair, making your character look up and down, or shooting a bullet ?
let me clarify it. basically my problem is about shooting a bullet. I already can shoot. but it doesn't go according to the crosshair. it just goes forward z-axis. the crosshair is actually in the center of the screen as like 1st person shooter. So, maybe the problem is to synchronize the bullet direction with crosshair. is it clear enough? sorry if my words is hard to be understood.
var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, desiredrotation); in this case, i want to make the desiredrotation according to the mouse-Y. So, the crosshair can be up and down, and if it's up/down, then the bullet will be launched to that crosshair's position. How do i do that? Anyway, thanks for replying. it's very helpful.thanks.
â anon28134237I don't quite understand what you want to do. Do you have a problem in displaying a crosshair, making your character look up and down, or shooting a bullet ?
â lhklet me clarify it. basically my problem is about shooting a bullet. I already can shoot. but it doesn't go according to the crosshair. it just goes forward z-axis. the crosshair is actually in the center of the screen as like 1st person shooter. So, maybe the problem is to synchronize the bullet direction with crosshair. is it clear enough? sorry if my words is hard to be understood.
â anon28134237