I'm trying to convert the Vector data from Input.mousePosition() relative to the playing screen. It works without the camera.ScreenToWorldPoint line but not properly. However, it doesn't work at all with that line in, saying I need to a attach a camera (there is one as a child to the player).
Here's my code thus far:
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var mousePosition;
var AllowClickJump : boolean = true;
var clickJump : float = 2;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function Awake()
{
clickJump = clickJump/10;
}
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// mouse jump
/* HERE IS WHERE MY PROBLEM IS */
if (Input.GetButtonDown ("Fire1") && AllowClickJump)
{
mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
//THIS LINE RIGHT HERE kills the script, saying I need
//to attach a camera.
mousePosition = camera.ScreenToWorldPoint(mousePosition);
mousePosition = transform.TransformDirection(mousePosition);
moveDirection = mousePosition;
moveDirection *= clickJump;
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)