Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by leonalchemist · Feb 26, 2011 at 04:37 PM · bugmouseflashlight

Flashlight looking at mouse in 2D

Simple code, im trying to make a cube follow the position of the mouse, i tried different things but something's not right; the position of the cube is far from the actual position of the mouse pointer, why is that?

//GameObject is the cube
void Update () 
{
    gameObject.transform.position = Input.mousePosition;
}

or in javascript:

var mousePos : Vector3;

function Update () { mousePos = Input.mousePosition; gameObject.transform.position = mousePos; }

or just using the lookAt function to make it simpler

transform.LookAt(Input.mousePosition);

im doing this so that i can target a flashlight torwards the cube, this is a 2D game so the MouseLook script wont work; i keep seeing there's something to do with the screen.height but im not rly sure what to do. thx u

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Alec-Slayden · Feb 26, 2011 at 05:14 PM

The mouse operates in screen space, a 2-dimensional(basically) plane in front of the camera, it uses different coordinates and isn't really a 3D thing you can look at.

However, you can have your code turn it into world coordinates by properly using ScreenToWorldPoint

This will make a Vector3 that you can use as position information (specifying your own z coordinate).

for example,

transform.position = myCamera.ScreenToWorldPoint( Vector3(Input.mousePosition.x, Input.mousePosition.y,10));

will set your object's position 10 units directly in front of the camera, at the mouse position.

EDIT:

I changed the code to imply the camera as a variable. You can declare this with your other variables

var myCamera : Camera;

and in the inspector, drag the camera game object into that variable slot.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Feb 26, 2011 at 06:08 PM 1
Share

Well, 10 is a bit far... since 1 unit == 1 meter. The example on ScreenToWorldPoint suggest to use camera.nearClipPlane. But keep in $$anonymous$$d the camera clips at the near plane so you will need to move it a bit further which depends on your object size. If you just want to move a not rendered GameObject eg. with a spotlight then use camera.nearClipPlane.

avatar image leonalchemist · Feb 27, 2011 at 01:01 AM 0
Share

i rllllyyyy dont get it...

for one yea the mouse operation in a 2D manner but thats what i want anyway, cant u just leave the z coordinate to a static number or something?

and y does the above code i wrote make the object like +300 in the x axes and +500 in the y axes (random number) i mean why isnt it on top of the actual mouse?

and err, wat r world coordinates anyway? wats the difference to just giving it normal Vector3 coordinates?

also i get an error i need to attach a camera to the object that's following the mouse?

sry new to all this.

avatar image Alec-Slayden · Feb 27, 2011 at 04:40 AM 0
Share

Yeah thats a good point, Bunny; I was just putting in an arbitrary distance but 10 probably would be far in most scenes :D

@Leon the problem is that the mouse isn't operating in the world space. The mouse's X position is a special number unique to the screen, it's different from the X position of objects in the game.

In order to make it be on top of the actual mouse, you need to have the game interpret the screen points into world points. Thats what the code I provided above does. I'll add in an edit about the camera.

avatar image leonalchemist · Feb 27, 2011 at 01:27 PM 0
Share

mmm took me a $$anonymous$$ute to get it, it does kinda work but i dont think the way im doing it is right, im not sure why also the light keeps shaking quite a lot, and i still need to write something for the rotation of the flashlight + make it stay on the character, isnt there a simpler way to make a flashlight system for 2D games? thx for the help anyway.

avatar image Alec-Slayden · Feb 27, 2011 at 06:29 PM 0
Share

what you should probably do for a flashlight is make the actual object a child of the character you're moving around, and add a spotlight. If you give this spotlight a flashlight cookie (which I believe there are a couple included with Unity's packages). I'm not sure the sort of effect you're going for, but a spotlight is probably going to be handy

avatar image
0

Answer by farzamvat · Aug 13, 2015 at 02:23 AM

the position of mouse is in pixel form and it's discrete, you should convert mouse position to unity's world with Camera.main.ScreenToWorld(Input.mousePosition) function ;)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Known issue with Unity Editor on Surface Pro? 3 Answers

My mouse cursor disappears when I press play! 3 Answers

Raycast hits both objects 1 Answer

How to give support for Samsung S-Pen? 1 Answer

2D Mouse and Right analog as Direction for the flashlight. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges