Hi, I’m working on a game that you can cast spells, but the camera is following from behind the character. I want the character to be able to turn left and right with the camera rotating along with it, but i only want the cursor to move up or down so they can click where they want to cast the spell from the middle of the screen. Any suggestions on how to do this with cursorlock or other methods?? Thank you
2 Answers
2I hate when games have their own custom cursor that reads your mouse position 'cause it always seems a little laggy, but it would be useful in this case.
You can have a vector for the mouse position, set a GUI texture of a mouse cursor to that position, and when required limit the desired axis so regardless of where the mouse is it will appear to be in the center. Then get all of your click info from the GUI cursors position rather than the actual mouse cursor.
I won’t be able to test it but here’s a gist of what I mean:
public Texture2D mouse_cursor;
private Vector3 mouse_pos;
private Rect mouse_rect;
public bool limitaxis = false;
void Update(){
mouse_pos = Input.mousePosition;
mouse_pos.z = 0;
//I think screen coords may need to be
//changed to properly fit GUI but I'm not 100% sure
mouse_pos.y = Screen.height - mouse_pos.y;
mouse_pos.x = Screen.width - mouse_pos.x;
if(limitaxis){
mouse_pos.x = Screen.width * 0.5f; //Stick to center of screen
}
mouse_rect = new Rect(mouse_pos.x, mouse_pos.y, someSize, someSize);
}
void OnGUI(){
GUI.DrawTexture(mouse_rect, mouse_cursor, ScaleMode.ScaleToFit, true, 0);
}
I can’t test this so some of the calculations might be wrong but the concept should work. You might want to subtract someSize from each position, but if you want the very top left tip of the cursor to click (depending on your image) keep it how it is.
You can hide the cursor using :
Now just draw your spell casting icon using a gui texture with the mouse y position and x = center of screen.
I like this approach as well. I may test this one and see which is working better for me.
– relicrightI may be mistaken, but I'm pretty sure hiding the cursor automatically locks it to the screen center
– Josh707