x


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;
}
more ▼

asked Sep 11 '12 at 08:04 PM

Hordaland gravatar image

Hordaland
61 3 5 8

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.

Sep 12 '12 at 12:38 PM Hordaland
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x50
x15
x1

asked: Sep 11 '12 at 08:04 PM

Seen: 402 times

Last Updated: Sep 12 '12 at 12:38 PM