Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Adam Bruns · Feb 21, 2010 at 08:43 AM · 2drotationmouse

object rotates toward mouse? 2D Top Gameplay

I'm Making a 2D game, from the top view, I have it so you can walk around and the camera follows (fyi the player is a sphere atm). I want it so that the gun will rotate around the sphere, pointing at wherever the mouse is, I originally tried:

var mouseX;
var mouseY;
function Update () {
mouseX = Input.mousePosition.z;
mouseY = Input.mousePosition.y;
transform.Rotate(Vector3(mouseX,mouseY,0));
}

but that did not work (it spun out of control on all axis, i dont want it to go up and down (y axis) rotate in a circle on the z and x axis). i looked at other posts and found this (it was for a 2D side view)

Vector3 mousePos = Input.mousePosition; mousePos.z = -(transform.position.x - Camera.mainCamera.transform.position.x);

Vector3 worldPos = Camera.mainCamera.ScreenToWorldPoint(mousePos);

transform.LookAt(worldPos);

but i have no idea what that stuff means nor how to use it if someone could help me get a code and explain how it works, or change one of these it would greatly help

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

6 Replies

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

Answer by Sebas · Feb 21, 2010 at 09:36 AM

Try this code. It will determine the World Position where your mouse cursor is at the exact height of your character. The character now always looks at this point which means the character faces your mouse cursor at all times.

//attach this script to your camera and drag your character GameObject in the fpc slot in the inspector

private var worldPos : Vector3; private var mouseX : int; private var mouseY : int; private var cameraDif : int; var fpc : GameObject;

function Start () {

 //determines how far down the ScreenToWorldPoint is from the camera position
 //it's calculated [height of camera] - [height of pivot point of character]
 //this is to ensure the character only rotates (via LookAt) along rotation.x and doesn't look up or down
 cameraDif = camera.transform.position.y - fpc.transform.position.y;


}

function Update () {

 mouseX = Input.mousePosition.x;
 mouseY = Input.mousePosition.y;


 //this takes your current camera and defines the world position where your mouse cursor is at the height of your character -->translates your onscreen position of mouse into world coordinates
 worldPos = camera.ScreenToWorldPoint(Vector3(mouseX, mouseY, cameraDif));

 fpc.transform.LookAt(worldPos);

}

Comment
Add comment · Show 1 · 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 KrazyKain · Mar 01, 2010 at 01:41 AM 0
Share

thanks alot man, this was a problem for me too :)

avatar image
0

Answer by VadimNavr · Oct 04, 2010 at 03:27 PM

I use this script, my person follows the mouse cursor, but it looks at the floor. May be it is because of my game is 3D...

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
avatar image
0

Answer by Quest · Nov 03, 2010 at 03:09 PM

Omg finally found what i want searchewd months for it but uhm... how does it work ? what is Fpc and why he is constantly saying there is no camera attached to gameObject but there is ! can someone help me ? what to do ? i got a object called Tank> tank gots a body wheels camera and turret i want the turret to move what do i have to give the script and what do i have to give the variable Fpc ? could someone please help me ?

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
avatar image
0

Answer by BranDino · Jun 05, 2013 at 11:23 PM

The answer to your final question: I think there is a slight spacing error in the code on line 8 above, delete the space btwn the colon and Gameobject var fpc : GameObject; so reads var fpc :GameObject; Once space is deleted, save and drag this script onto your camera, now in the inspector you will see a variable labeled fpc , now drag your game object to the right of that variable into the inspector, this will apply the mouse movement to that object.

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
avatar image
0

Answer by v0kb0l0k · Oct 13, 2014 at 09:50 PM

May be something like:

 var currentLookVector = mousePosWorld - transform.position;
 currentLookVector.z = 0;
 currentLookVector = currentLookVector.normalized;
 transform.rotation = startRotation * 
 Quaternion.FromToRotation(startLookVector, currentLookVector);

startRotation is rotation from what rotation start and startLookVector is vector when mouse pressed down.

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
  • 1
  • 2
  • ›

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how do i make an object always face the player? 5 Answers

SIDE SCROLLING MOUSE CONROL HELP! 0 Answers

Start Coroutine after other has finished 4 Answers

Smoothed Rotation with 2D Top-Down View 1 Answer

I need help with clamping a rotation towards the mouse position 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