- Home /
Why my bullet does not rotate via Z axis?
Hello. I am new to unity and i have discovered a problem. I want to create bullet, then rotate it towards mouse position, and after that shoot it in that dircetion. I cant find a fix to this problem for week, maybe you will help. So the problem is that the bullet does not rotate via z axis . Instead of this it rotates via x and y axis, and i dont know why. Thanks for help. Here is my code:
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Input.mousePosition;
mousePos = cam.ScreenToWorldPoint(mousePos);
Vector2 direction = new Vector2(
mousePos.x - Gracz1.transform.position.x,
mousePos.y - Gracz1.transform.position.y
);
Instantiate(bullet, Gracz1.transform.position, Quaternion.identity).GetComponent<Transform>().LookAt(direction);
}
Answer by GamitusLabs · Nov 15, 2018 at 07:24 PM
YES! Thank you so much! I had to modify the code a bit to make it work but i done it (finally) Here is my code:
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
Vector3 mousePos = Input.mousePosition;
mousePos = cam.ScreenToWorldPoint(mousePos);
v_diff = (mousePos - transform.position);
atan2 = $$anonymous$$athf.Atan2(v_diff.y, v_diff.x);
Quaternion direction = Quaternion.Euler(0f, 0f, atan2 * $$anonymous$$athf.Rad2Deg + 90);
Instantiate(bullet, Gracz1.transform.position, direction);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612182210im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem Rotating up vector of camera to mouse position on screen. 2 Answers
Make a Player Model Rotate towards Mouse Location 3 Answers
Rotate player to aim on one axis (Z-axis) towards mouse position/joystick - 2.5D (3D) 0 Answers
Rotate object based on mouse position 1 Answer
Trying to make an object face the mouse, but switch sides of player based on its rotation? 0 Answers