(Roblox.TS) Cannot get camera-based angle detection working when the mouse is locked

  1. What do you want to achieve? So you probably know the angle detection system Mount and Blade has, example if you move your camera to up, you will slash your weapon on that angle.

Photo attached for clarification:

  1. What is the issue? The issue is that, it literally does not work. When I do not lock my mouse, it works, however I wanna lock my mouse and keep my way of calculating which angle is being used

  2. What solutions have you tried so far? I tried using different methods provided in Developer Forum, tried using AI however no avail.

By the way, I locked my mouse like this: UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter;

Here is what I got so far trying to implement this combat system when the mouse is locked to center:

import { RunService, UserInputService } from "@rbxts/services";
import { Players, Workspace } from "@rbxts/services";


const player = Players.LocalPlayer;
const RightLeft = player.WaitForChild("PlayerGui").WaitForChild("MainGUI").WaitForChild("Hover").WaitForChild("RightLeft") as TextLabel;
const UpDown = player.WaitForChild("PlayerGui").WaitForChild("MainGUI").WaitForChild("Hover").WaitForChild("UpDown") as TextLabel;

function getAngle(x1: number, y1: number, x2: number, y2: number): number {
    return math.atan2(y2 - y1, x2 - x1);
}

function setAllArrowsDisabled(): void {
    RightLeft.Text = "";
    UpDown.Text = "";
}

function getDirection(angle: number): string {
    angle = math.deg(angle);

    if (angle >= -45 && angle < 45) {
        setAllArrowsDisabled();
        RightLeft.Text = ">";
        RightLeft.TextXAlignment = Enum.TextXAlignment.Right;
        return "right";
    } else if (angle >= 45 && angle < 135) {
        UpDown.Text = ">";
        UpDown.TextXAlignment = Enum.TextXAlignment.Right;
        return "down";
    } else if (angle >= 135 || angle < -135) {
        RightLeft.Text = "<";
        RightLeft.TextXAlignment = Enum.TextXAlignment.Left;
        return "left";
    } else {
        UpDown.Text = "<";
        UpDown.TextXAlignment = Enum.TextXAlignment.Left;
        return "up";
    }
}


RunService.RenderStepped.Connect(() => {
    const camera = Workspace.CurrentCamera;
    if (camera) {
        const cameraRotation = camera.CFrame.LookVector;
        const mouseDelta = UserInputService.GetMouseDelta(); 
        const angle = getAngle(0, 0, cameraRotation.X + mouseDelta.X, cameraRotation.Z + mouseDelta.Y);
        const direction = getDirection(angle);
        print(direction); 
    }
});

All it returns is “Up” or whatever I aim at start then it does nothing and that is probably because latency, the mouse was not locked yet. It does not work when it is locked.

Any help would be welcome.

bumping this topic

[quote=“diagrammatized, post:1, topic:2812872, full:true, username:diagrammatized”]

  1. What do you want to achieve? So you probably know the angle detection system Mount and Blade has, example if you move your camera to up, you will slash your weapon on that angle.

Photo attached for clarification:

  1. What is the issue? The issue is that, it literally does not work. When I do not lock my mouse, it works, however I wanna lock my mouse and keep my way of calculating which angle is being used

  2. What solutions have you tried so far? I tried using different methods provided in Developer Forum, tried using AI however no avail.

By the way, I locked my mouse like this: UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter;

Here is what I got so far trying to implement this combat system when the mouse is locked to center:

import { RunService, UserInputService } from "@rbxts/services";
import { Players, Workspace } from "@rbxts/services";


const player = Players.LocalPlayer;
const RightLeft = player.WaitForChild("PlayerGui").WaitForChild("MainGUI").WaitForChild("Hover").WaitForChild("RightLeft") as TextLabel;
const UpDown = player.WaitForChild("PlayerGui").WaitForChild("MainGUI").WaitForChild("Hover").WaitForChild("UpDown") as TextLabel;

function getAngle(x1: number, y1: number, x2: number, y2: number): number {
    return math.atan2(y2 - y1, x2 - x1);
}

function setAllArrowsDisabled(): void {
    RightLeft.Text = "";
    UpDown.Text = "";
}

function getDirection(angle: number): string {
    angle = math.deg(angle);

    if (angle >= -45 && angle < 45) {
        setAllArrowsDisabled();
        RightLeft.Text = ">";
        RightLeft.TextXAlignment = Enum.TextXAlignment.Right;
        return "right";
    } else if (angle >= 45 && angle < 135) {
        UpDown.Text = ">";
        UpDown.TextXAlignment = Enum.TextXAlignment.Right;
        return "down";
    } else if (angle >= 135 || angle < -135) {
        RightLeft.Text = "<";
        RightLeft.TextXAlignment = Enum.TextXAlignment.Left;
        return "left";
    } else {
        UpDown.Text = "<";
        UpDown.TextXAlignment = Enum.TextXAlignment.Left;
        return "up";
    }
}


RunService.RenderStepped.Connect(() => {
    const camera = Workspace.CurrentCamera;
    if (camera) {
        const cameraRotation = camera.CFrame.LookVector;
        const mouseDelta = UserInputService.GetMouseDelta(); 
        const angle = getAngle(0, 0, cameraRotation.X + mouseDelta.X, cameraRotation.Z + mouseDelta.Y);
        const direction = getDirection(angle);
        print(direction); 
    }
});

All it returns is “Up” or whatever I aim at start then it does nothing and that is probably because latency, the mouse was not locked yet. It does not work when it is locked.

bumping

bump (this is a cool bump, right)

Nevermind, solved it by myself.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.