Hello everyone,
I hate asking for help but I am really stuck in a corner with my game. I been working on a game for a while now originally in XNA and now I am redeveloping in Unity. But I am having some trouble with the physics of the game. I need to rotate bullets/shots and my cannon towards the mouse position in a 3D space, see the screenshot of the game of what I am doing please:
http://www.freeimagehosting.net/newuploads/4vxkq.jpg
The cannon positioning is decent, but I can’t get the shots to line up correctly. They move They travel in the correct direction towards the mouse for the most part, but they won’t rotate correctly, see the screenshot. They should point towards the mouse and they are getting rotated a different direction. For the shots, I am using this code:
`
// Rotating the shot, this happens only once when the shot is first fired so it doesn’t glue to the mouse
Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 3.7f);
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = new Vector3(
playerController.transform.position.x, playerController.transform.position.y - .1f, playerController.transform.position.z);
transform.LookAt (mousePosition);
// The code used to move the shot
rigidbody.AddForce(transform.forward * velocity);
rigidbody.position = new Vector3(transform.position.x, transform.position.y, -3.7f);
`
Notice in the rigidbody position, it forces me to add the -3.7… this is the position away from 0,0,0 on the grid that the tank is located at. Without that line of code, the shot travels wanderessly out towards the sky on the Z axis…
Why is this happening? I like unity but I’m on the fence with all of these physics functions, lookat and etc. it’s just not working right. I read documentation, I tried tons and tons of code on these forums, nothing works correctly. I already fix the rotation to what I want but then Unity screws it up with rigidbody.addforce.
Basically all I want to do is move the shots on the X and Y transform axis rotated while keeping the shot locked on the Z axis. I tried this code originally (from my XNA port):
x += Mathf.Cos(velocity * angle);
y -= Mathf.Sin(velocity * angle);
So I tried the rigidbody add force. This would be fine if the rotation of the shots didn’t get screwed up. Right now those are just tiny machine gun bullets but I want to have straight line laser blast and rockets and they HAVE to rotated correctly towards where the mouse is. Can anyone help with this? Some more things…
The camera is -5 on the Z axis and the ship is -3.7 on the z axis. I’m confused as to what to do with the “z” axis of the mouse when using the camera screentoworldpoint function. It works when I put -3.7 but now for the camera’s -5 axis.