- Home /
Help implementing aiming in sidescroller via mouse.
I am creating a sidescroller platformer in Unity3D and using a custom controller. I ultimately want the character to move left and right via the a and d keys, but my face direction and aiming to be controlled with the mouse. I have movement and facing with the mouse taken care of. If it helps this is a snippet of how this works:
moveDirX = Input.GetAxisRaw("Horizontal");
if (!sliding) {
float speed = (Input.GetButton("Run"))?runSpeed:walkSpeed;
targetSpeed = moveDirX * speed;
currentSpeed = IncrementTowards(currentSpeed, targetSpeed,acceleration);
// Face Direction using mouse
if (Input.mousePosition.x > (screenX/2)){
transform.eulerAngles = Vector3.up*180;
}
if (Input.mousePosition.x < (screenX/2)){
transform.eulerAngles = Vector3.zero ;
}
Though I do not know the best way to manage aiming. I do not want a mouse cursor on screen. The way I imagine it, there is a reticle at a fixed distance from the characters front (you cannot shoot behind you) and when you move the mouse up or down the reticle (and the characters arms and weapon) pivots or rotates upward or downward. Then when I fire with left mouse button my projectile fires at the angle the reticle/weapon is pointing. I would also need to limit the range the weapon can point up or down.
The reticle may not end up in the game, its just how I imagine it to function. Would I have a ray cast from the end of the weapon and rotate the weapon via the mouse movement? I do not need finished code to copy and paste in; I want to know how everything works. I am just looking for suggestions of possible efficient ways to perform this.
Your answer
![](https://koobas.hobune.stream/wayback/20220613183317im_/https://answers.unity.com/themes/thub/images/avi.jpg)