Hi guys. I’m trying to get 2D mouse aiming working, so I can make a grappling hook, and I’m having a few issues.
The red line that you can see is from point 0,0,0 to the player. The blue line is from the origin, to what is supposed to be the mouse cursor. The green line is a raycast, from the player through the mouse, and is supposed to collide with solid objects (which, admittedly, is what it’s doing.)
However, as you can see from the actual active part of the screenshot, where the raycast is going, and where the mouse is pointing, is two different things. I can’t work out why. I’m guessing it has to do with how I’m grabbing the mouse data, though.
I’ll also post my grapple.js, so maybe someone can work out what the heck I’m doing wrong.
Also, forgive me if there’s already an answer somewhere on this, I couldn’t find it.
private var hit : RaycastHit;
var testBox : GameObject;
private var lastPosition : Vector3; // Where the mouse position was last
private var mouseRel : Vector3;
function Start()
{
lastPosition = Input.mousePosition;
mouseRel = Vector3.zero;
}
function Update () {
var mousePos = Input.mousePosition;
// If the mouse has moved since the last update
if (mousePos != lastPosition) {
lastPosition = mousePos;
mPosX = mousePos.x;
mPosY = mousePos.y;
mouseRel = Vector3(mPosX, mPosY, 0);
}
Physics.Raycast (transform.position, mouseRel, hit);
if (Input.GetMouseButtonDown(0)) {
//Physics.Raycast (transform.position, mouseRel, hit);
Instantiate(testBox, mouseRel, Quaternion.identity);
}
DebugDrawLine(transform.position, hit.point, Color.green);
DebugDrawLine(Vector3.zero, mouseRel, Color.blue);
DebugDrawLine(Vector3.zero, transform.position, Color.red);
}
function DebugDrawLine(playerPos: Vector3, mousePos: Vector3, lineCol: Color) {
Debug.DrawLine (playerPos, mousePos, lineCol);
}