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
2
Question by Sowel · Nov 20, 2010 at 03:45 AM · mouselookattopdown

need help with making player look at mouse

Im trying to make a top down shooter and i am stuck on getting the player to rotate to look at where the mouse is on the screen. i have searched google for a help but all the scripts i found make my player spin out of control in all directions.

if some one could show me the script i would really appreciate it... or if you can point me in the direction of a top down shooter tutorial that would be even better =] ....EDIT.....

thanks for the replies, right now i am using this script.

 var rotateSpeed = 5.0;
 var hitdist = 28.0;
 
 function Update () 
 
 {   
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var targetPoint = ray.GetPoint(hitdist);
     var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation,
     rotateSpeed * Time.deltaTime);
 }


now i am trying to work out how to make it only rotate alone one axis to stop the player aiming up and down as the mouse moves towards and away the player.

....EDIT2....

i found out how to do it.

 var speed = 4.0;
 
 function Update () {
 
     var playerPlane = new Plane(Vector3.up, transform.position);
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hitdist = 0.0;
 
     if (playerPlane.Raycast (ray, hitdist)) {
 
         var targetPoint = ray.GetPoint(hitdist);
         var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
 
         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
     }
 }


once i work out the rest of my problems to get a simple topdown prototype i will upload my assets for others to use. it will be a useful starting point to speed up developing a game

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

3 Replies

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

Answer by duck · Nov 20, 2010 at 09:16 AM

Generally you will need to do three steps:

  1. Convert the mouse's screen position into a world-space ray
  2. cast the ray into your scene and find the ray's intersection point
  3. Use the ray's hit point as the target for LookAt

Eg:

var lookTarget : Vector3;

function Update() { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit)) { lookTarget = hit.point; }

 transform.LookAt(lookTarget);

}

If you want your object to gradually turn towards the target instead of snapping its rotation, you could modify the script like this:

var lookTarget : Vector3; var turnSpeed : 20.0;

function Update() {

 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var hit : RaycastHit;
 if (Physics.Raycast (ray, hit) {
     lookTarget = hit.point;
 }

 // rotate towards target
 var lookDelta = (hit.point-transform.position);
 var targetRot = Quaternion.LookRotation(lookDelta);
 var rotSpeed = turnSpeed * Time.deltaTime;
 transform.rotation = Quaternion.RotateTowards( transform.rotation, targetRot, rotSpeed );

}

(untested code - leave comments if you find typos!)

Comment
Add comment · Show 2 · 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 GregIven · Nov 19, 2015 at 03:21 AM 0
Share

I am just giving a revision, make sure to put the keyword "out" in front of hit to get the physics.Raycast() function to work.

 if (Physics.Raycast (ray, out hit)
avatar image Eno-Khaon GregIven · Nov 19, 2015 at 03:35 AM 0
Share

Countering your revision, the script described is written in Javascript. While the format in C# DOES call for the "out" keyword, it is not used in JS.

For each version (from the documentation example):

 // C#
 if (Physics.Raycast(transform.position, -Vector3.up, out hit))

and...

 // Unityscript
 if (Physics.Raycast (transform.position, -Vector3.up, hit))
avatar image
1

Answer by denewbie · Nov 20, 2010 at 04:39 AM

I'm not sure how your game really works but if you just want something to look at your mouse then:

1) Use this to get the mouse position: Input.mousePosition

2) Use THIS to get the 3D world position for the given mouse position:

     var MouseScreenPosVector3 : Vector3 = camera.ScreenToWorldPoint (Vector3 (Input.mousePosition.x, Screen.height - Input.mousePosition.y,camera.nearClipPlane));

3) Finally, use this to make the mouse look at that position:

         transform.LookAt(MouseScreenPosVector3);   // where 'transform' is the transform of your player 'MouseScreenPosVector3' is the Vector3 position of the mouse positions you attained in step 2
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 Jesse Anders · Nov 20, 2010 at 04:14 AM

Look for the 'Evac City' tutorial (it covers this).

As for how to code it, there's no 'the script', per se. Typically, however, the solution will involve getting the mouse position and the player position into the same space (e.g. world space or screen space) and then using Atan2() or Quaternion.LookRotation() to (directly or indirectly) align the player towards the cursor.

If you have some code already but it's not working, consider editing your post to include that code (someone may be able to spot the problem for you).

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

simple camera script 0 Answers

Top down shooter problem look exactly at mouse 0 Answers

Lookat mouse, without raycasting? 1 Answer

Top-Down 2D game:How to make the camera move on hitting the sides of the screen? 2 Answers

top down shooter help 0 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