x


camera.ScreenToWorldPoint() question

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)

more ▼

asked Jun 16, 2010 at 01:29 AM

karl_ gravatar image

karl_
3k 115 111 138

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

If you don't have a camera attached to the Game Object, of course it won't work. "camera" is just a shortcut for GetComponent(Camera), not GetComponentInChildren(Camera). Just add this at the top of the script and drag your camera's Game Object onto it.

var theCamera : Camera;

Then, change your trouble line to

mousePosition = theCamera.ScreenToWorldPoint(mousePosition);

Call it whatever you like if you don't like "theCamera". I like using C# better for this, because you can still call it "camera", you just need to use

new public Camera camera;
more ▼

answered Jun 16, 2010 at 02:03 AM

Jessy gravatar image

Jessy
18.3k 208 178 293

Exactly what I was missing. Stupid mistake. Thanks for the quick answer.

Jun 16, 2010 at 02:49 AM karl_
(comments are locked)
10|3000 characters needed characters left
Your answer
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:

x1450
x153

asked: Jun 16, 2010 at 01:29 AM

Seen: 5027 times

Last Updated: Jun 16, 2010 at 01:29 AM