- Home /
Moving around a weapon on screen using OnMouseDrag
Hey dudes. I'm creating a first person game where the player can move around his weapon on the screen. Basically you're just supposed to hold down a button (so far I'm using LMouse but RMouse will be the final button) and move the mouse and the camera will freeze and the weapon will move instead of the camera.
I've gotten the weapon to move and all using a plane with an OnMouseDrag-function but first of all it's moving way too slow (I want it to move at the same speed as the mouse) and secondly it's jumping back and forth 1 frame at a time when I move the character around while holding the mouse button down.
Any tips on how to straighten things up? Is my method using the plane void?
Here's the code: #pragma strict
var cam : Camera;
var controller : GameObject;
var sword : GameObject;
var swordDrag : boolean;
internal var gameSens : float;
internal var screenPosition : Vector3;
function Start()
{
gameSens = cam.GetComponent(MouseLook).sensitivityY;
Screen.lockCursor = true;
screenPosition = cam.WorldToScreenPoint(transform.position);
}
function OnMouseDown()
{
swordDrag = true;
cam.GetComponent(MouseLook).sensitivityY = 0;
controller.GetComponent(MouseLook).sensitivityX = 0;
}
function OnMouseUp()
{
swordDrag = false;
cam.GetComponent(MouseLook).sensitivityY = gameSens;
controller.GetComponent(MouseLook).sensitivityX = gameSens;
}
function OnMouseDrag()
{
if (swordDrag == true)
{
//Read the mouse input axes
screenPosition.x += Input.GetAxis("Mouse X");
screenPosition.y += Input.GetAxis("Mouse Y");
//move the object to the world position to not change screen position
sword.transform.position = cam.ScreenToWorldPoint(screenPosition);
}
//else return;
}
The "jumping back and forth" (that is the main problem) almost seems to create two swords, one that stays in the right place and flicker every frame and another one that also flickers every frame and kinda moves around based on how I move my character. If I strafe left the sword moves right and vice versa. I don't know if it's because the camera points at a different spot on the plane or what. Can anyone think of a better way to get the result I want? You don't need to give me the entire code or anything, just a pointer in the right direction.
Your answer