2D shooting script fixed camera
Hello everyone I'm working on a 2D top down shooter game with fixed camera in the middle of each rooms but this leads me to an issue to shot towards the cursor mouse postion
I'm currently having this script on my bullets prefab :
public float amount;
Vector3 direction;
void Start()
{
Vector3 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.LookRotation(Vector3.forward, direction - transform.position);
direction = direction.normalized;
GetComponent<Rigidbody2D>().AddForce(direction * amount);
}
Then I have a little piece of code to instanciate my bullets on left click in my player script. But when I move my character if I shoot between the camera and the player there's some troubles. (going in a wrong direction)
I guess it is because of the way I'm getting the mouse position but
I have no idea on how to fix that. Hope you could help me :)
Answer by TreyH · Feb 12, 2016 at 11:55 PM
@Nathuram You can do this in a few different ways. A raycast would be the most straight forward, but it would require your mouse cursor actually hovering over something with a collider (which is not typically an issue as you can just set them to a physics layer specifically for this kind of thing).
A collider-free implementation of raycasting for your problem:
// Assumptions:
// - We are in 2D mode
// - The main Camera is in the negative Z-Axis looking forward
//
// We'll return similarly to how a Raycast works
bool MouseRelativeForward(Transform focus, out Vector3 relativeForward)
{
// Create a ray from the camera in the direction of our mouse position
Ray worldRay = Camera.main.ScreenPointToRay(Input.mousePosition);
// Vertical distance between us (this will be Z in 2D games)
float heightDelta = focus.position.z - worldRay.origin.z;
// Make sure we're above the object
if (heightDelta > 0)
{
// Need the proportion of this distance relative to the height component
float rayMagnitude = heightDelta / worldRay.direction.z;
// Project this point onto the same horizontal plane as the transform
Vector3 planarPoint = worldRay.origin + worldRay.direction * rayMagnitude;
// Look at the object
relativeForward = (planarPoint - focus.position).normalized;
// Return it here
return true;
}
// If we didn't have a correct height
relativeForward = Vector3.zero;
// Let user know we didn't calculate anything
return false;
}
// Debug for confirmation
void OnDrawGizmos()
{
// Check our function
Vector3 relative;
// This returns whether or not we have a valid direction
bool success = MouseRelativeForward(this.transform, out relative);
// If so, print the direction
if (success)
{
Gizmos.DrawLine(this.transform.position, this.transform.position + relative * 100);
}
}
Answer by gorbit99 · Feb 11, 2016 at 05:28 PM
direction += yourCamera.transform.position * new Vector3(1, 0, 1);
I don't understand what this should achive, multiply the values of the X and Z by 1 in the Camera position? By the way the syntaxis of this doesn't work for me the transform.position seems to need {get, set}, moreover the operator * doesn't exist for camera and neither for transform from what i saw in the doc.
You add the cam position to the direction after you set it to the mouseposition, and the multiplication is to negate the y value of the vector
I tried with the * but as I said this operator doesn't exist so i used the Vector3.scale to multiply my direction by a Vector (1,0,1) this doesn't change anything at all to my problem.
I think that it's because of the fact that the camera position is involved to get the mouse cursor position.
I belive that using my BulletSpawn game object to create my direction vector would be the key to solve that but so far I have no clue on how to do so because I didn't succed to get back the mouse position without the Camera.main.ScreenToWorldPoint(Input.mousePosition).