- Home /
How to make in-game weapon aim at location of mouse?
I have a system where the weapon on the player object is meant to be aimable seperately to the actual movement of the object, though still within what's currently invisible on screen, aiming at wherever the mouse currently is.
As it stands, though, it only points relative forward, and stops following the rotation of the player entirely when I try certain things. Does anyone have a good method of making the weapon object rotate/aim based on the position of the mouse? As it stands i've been trying a mix of Quaternion.RotateTowards and Camera.main.ScreenToViewportPoint(Input.mousePosition), but this isn't working (quick thought: Do I need the UI code block for this to function?).
Full section of code below:
void Update()
{
//shotLoad is the script name for the weapon object, where projectiles are being launched from, just in front of the player.
shotLoad.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(Camera.main.ScreenToViewportPoint(Input.mousePosition)), Time.deltaTime * 10);
}
Answer by I_Am_Err00r · Jul 23, 2019 at 08:47 PM
public class ExampleClass : MonoBehaviour
{
// Angular speed in radians per sec.
float speed;
void Update()
{
Vector3 targetDir = Camera.main.ScreenToViewportPoint(Input.mousePosition) - transform.position;
// The step size is equal to speed times frame time.
float step = speed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
Debug.DrawRay(transform.position, newDir, Color.red);
// Move our position a step closer to the target.
transform.rotation = Quaternion.LookRotation(newDir);
}
}
Copied directly from here, slightly edited to what I think you are trying to do. Don't have a compiler to test, so let me know if that works.
This is much closer than i've been so far, so thanks. :)
Only issue now is that whilst the mouse is affecting the direction of the shots, they're all going off into somewhere in the mid to upper left area of the screen, to varying degrees based on mouse location.
I don't know how you project your bullets, but it has something to do with that script, change the projectile function to something like this:
bullet.transform.position = Vector3.$$anonymous$$oveTowards(bullet.transform.position, targetDir, Time.deltaTime);
The movement projection I was using was in a separate script (something i've accommodated for in your suggestion), attached to the projectile instances themselves, and relied on velocity:
rb.velocity = transform.TransformDirection(Vector3.forward * speed);
This never gave me direction for the shots, though the issue now is that what you've suggested may or may not be giving me direct for shots, but I can't tell because the bullets aren't moving. I've assumed this had something to do with how it's interpreting Time.deltaTime, but I can't be sure.
Your answer
Follow this Question
Related Questions
Rotate a crank with the mouse 0 Answers
Mouse axes based on position, not movement. 1 Answer
Player mouse rotation 2 Answers
3D Turret Rotation 0 Answers