• 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 FinKone · Jul 13, 2013 at 06:57 PM · third-personaim

Mouse orbit + ScreenToWorldPoint (LookAt) issues.

For those that are in the know - Line 36 might contain my problem.

Hey guys. I've been having a bit of trouble for the better part of the day with a control I'm trying to implement. I have a 3rd person camera set up, using a Orbit around the player, I'm trying to use a static cursor position (roughly centered on screen - and when I say static cursor position I'm talking about the 2D GUI AIMING texture, not the mouse... so I might be using wrong terms here.) and use ScreenToWorldPoint to get a position for my character to rotate to.

The reason for this is when the player draws his gun, and only then, the GUI2D Texture appears, and then from there I'm trying to get the 3D space for him to look at. I've tried to restrict the up/down motion and only have him face in the direction, but I can't even seem to get this far.

What current happens is the player warps to some odd ball angle ( I'm assuming because I'm not getting the correct position I want) and also he still changes in axis' that I thought I limited. The player also for the most part warps to face the screen...

         //Aimer
         public Texture2D aim;
         //Link to character controller to determine if weapon is out
         public ThirdPersonController WpnOut;
         //Store if weapon is out, and update it.
         private bool isWeaponOut;
         //Aimer size up and down
         private int aimW = 32;
         private int aimH = 32;
         //floats for storing mouse cords to make player look at (might change to int)
         private float mouseX;
         private float mouseY;
         //character ref so we can make him face the mouse postiion
         public GameObject player;
         //Storage of the variable that character will look at
         private Vector3 posToFace;
         //make sure our character only looks along the correct axis
         private float justTurn;
         
         
         
         void Start () {
             //ref to the AI script attacked for checking if weapon is out - if it is, over-ride current animations and use gun drawn animations.
             isWeaponOut = WpnOut.wpnDrawn;
             
             justTurn = camera.transform.position.y - player.transform.position.y;
         }
         
         void Update()
         {
             isWeaponOut = WpnOut.wpnDrawn;
             //Debug.Log (isWeaponOut);    
             
             if(isWeaponOut)
             {    
 //Right here might be my issue.  How can I use the GUI 2D texture to project what I need to look at straight away from the camera?
                 mouseX = Input.mousePosition.x;
                 mouseY = Input.mousePosition.y;
                 posToFace = camera.ScreenToWorldPoint(new Vector3(mouseX,mouseY,justTurn));
                 player.transform.LookAt(posToFace);    
             }
         }
     
         void OnGUI()
         {
             if (isWeaponOut){
                 //Static method.
                 GUI.DrawTexture(new Rect(Screen.width/2 + 200,Screen.height/2,aimW,aimH), aim);
                 //Non-static aim'er following mouse.
                 //GUI.DrawTexture(new Rect(Input.mousePosition.x -16 ,Screen.height - Input.mousePosition.y -16, aimW, aimH), aim);
             }
             else{return;}
         
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

1 Reply

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

Answer by supercouge · Jul 13, 2013 at 08:53 PM

I'm not sure to have understood what you wanted and I'm not a specialist, but...

     void Update () {
         Vector3 posToFace = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,Mathf.Max(camera.nearClipPlane,Vector3.Distance (player.transform.position,camera.transform.position)-1.0f)));
 
         player.transform.LookAt(posToFace);
         player.transform.localRotation = Quaternion.Euler(new Vector3(0.0f, player.transform.localRotation.eulerAngles.y, 0.0f));
 
     }


The Update() function take the mousePosition variable, then pass it to the ScreenToWorldPoint() function (the Z position is the distance from the camera to the player - 1.0f). This way, you have position of the mouse, in 3D space (the Z position being between the camera and the player).

Next, the player look at this 3D position (through LookAt()). Then, the rotation is blocked on X and Z axis. This allows the player to only turn on itself.

Comment
Add comment · Show 4 · 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 FinKone · Jul 13, 2013 at 09:18 PM 0
Share

$$anonymous$$an thats the desired state I was after (in regards to if I have it go after the mouse) nice. Going to be looking into and learning $$anonymous$$athf.$$anonymous$$ax, eulerAngles, and a few more things because these are clearly things I need to understand.

Also...

How can I get the position from casting a ray straight out from a 2d GUI texture as compared to using the mouse also? I'm not sure how to even word that search...

avatar image supercouge · Jul 13, 2013 at 09:42 PM 0
Share

Please, feel free to ask if you don't understand something. About your question, you want to cast a ray from which position to which position? (Anyway, check screenPointToRay() it may help you).

avatar image FinKone · Jul 13, 2013 at 09:52 PM 0
Share

From a 2D Texture (ai$$anonymous$$g/crosshair) that is at a static position on the screen to the location. I was thinking of changing from the mouse moving the cursor around, to making it so the mouse moves the screen around, but the cursor remains in the center of the screen.

So I think a pointtoray might be what I'm after. Either way you've been 100% helpful. Off the top of my head I can't really thing of anything - gonna go read up on the ref real fast about the 2 things I'm interested in, then look for more detailed information about $$anonymous$$athf and eulerAngles.

avatar image FinKone · Jul 13, 2013 at 10:02 PM 0
Share

Ok nice, so I went ahead and used a ScreenPointToRay, and this is what I'm after. I removed the ability for my cursor to move around, so it stays at the center - which was also throwing it off before. Now I'm just lacking the syntax I need to make the player transform and look at the point the ray hits...

Is there ever a day where ya' gurus feel like you've learned it all, or do ya' just always feel like ya' dont know anything?

Well I got it working, based off using a cursor lock kind of deal but its not a long term solution to me. Thanks again for all the help this turned into a hair puller.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

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

Related Questions

facing direction that does not return to zero 1 Answer

Aim to crosshair? 3 Answers

Objects with shared tags, delete currently selected? 2 Answers

Crosshair Aiming Issue, Simple, Yet Confusing. 1 Answer

Aim at touch (2D). 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