Bullet flying vertical
This code fires a bullet from enemy's gun towards camera, but the bullet stays vertical. How do I rotate the bullet's local coordinate system? Bullet is just a cylinder at the moment
void EnemyGunFire() {
Transform target = Camera.main.gameObject.transform;
Vector3 direction = (target.position - gun.transform.position).normalized;
GameObject bul = (GameObject)Instantiate(bullet, gun.transform.position, Quaternion.LookRotation(direction));
bul.GetComponent<Rigidbody>().velocity = direction * speed;
}
Answer by milosp · Aug 20, 2016 at 07:30 PM
I got it:
Transform cam = Camera.main.gameObject.transform;
Vector3 target = new Vector3(cam.position.x, cam.position.y, cam.position.z);
Vector3 direction = (target - gun.transform.position).normalized;
GameObject bul = (GameObject)Instantiate(bullet, gun.transform.position, Quaternion.identity);
bul.transform.rotation = Quaternion.FromToRotation(bul.transform.up, direction);
bul.GetComponent<Rigidbody>().AddForce(bul.transform.up * speed);
Answer by OsmiousH · Aug 20, 2016 at 10:43 AM
Try this
Change line 5 to
bul.GetComponent<Rigidbody>().AddForce(Vector3.Front);
You mean Vector3.forward * 100
? If I do that it travels forward in world coordinates. I also tried: bul.trasform.forward (and other) * 100
and
direction * 100
Your answer
Follow this Question
Related Questions
LookAt only on local Z Axis 0 Answers
how to reset back the rotation 0 Answers
How do I rotate my object back to the world's up direction whilst keeping it's forward direction? 0 Answers
Problem with fps touch look axis,Problem with look axis with touch field 0 Answers
How to find a position's angle around an arbitrary axis? 1 Answer