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 ProjectCryken · Feb 07, 2013 at 10:21 AM · mouseshooter

Top Down Shooter Aiming

Why would this not cause my player to rotate towards the mouse, assuming the camera is looking down on the player. Exactly vertical.

 var currentGun : GameObject;
 
 function Start () {
 
 }
 
 function Update () 
 {
     var position = Input.mousePosition;
     var newPosition = Vector3(position.x,position.y,camera.main.transform.position.y- currentGun.transform.position.y);
     var lastPosition = Camera.main.ScreenToWorldPoint(newPosition);
     transform.LookAt(lastPosition);
     
 }
Comment
Add comment · Show 6
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 robertbu · Feb 07, 2013 at 11:18 AM 0
Share

There is no way to deter$$anonymous$$e what is happening with this code since we don't have any idea what the position of currentGun is. This is nearly identical to your last post. As mentioned in your last post, use Camera.main.nearClipPlane for the Z value the Vector you pass to ScreenToWorldPoint() and your code should work.

avatar image ProjectCryken · Feb 07, 2013 at 11:34 AM 0
Share

No, im afraid it didn't.

avatar image robertbu · Feb 07, 2013 at 11:43 AM 0
Share

Then you likely have issues not related to this code. Run the sample code I posted in the other post and watch where the block are placed. $$anonymous$$ake sure the "transform" above is really set to the character you want to look at the camera.

avatar image ProjectCryken · Feb 07, 2013 at 11:58 AM 0
Share

Thanks for that, let me explain my set-up. I have taken the fps controller, removed mouse look and reposition camera above player. The above script is attached to the graphics of the controller, which is not affected by any other scripts. I want the roation and movement to be independent of eachother, this is why I have done this. $$anonymous$$y gun should rotate towards the mouse(it is attached to the graphics) but not rotate up or down which is why i want the z to be level with the gun.

avatar image robertbu · Feb 07, 2013 at 12:08 PM 0
Share

Then you calculate the position in world space first using nearClipPlane. After you have the point in world space, then you change the z coordinate to match z coordinate of your character (I think it's Z. Similar problems Tank turrets and guns reset the Y when looking down the z axis).

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Feb 07, 2013 at 11:09 AM

Because your player's orientation is not correct. Make sure your character's forward vector is really the forward you want, so it looks in the direction of the z axis (blue arrow).

Comment
Add comment · Show 6 · 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 ProjectCryken · Feb 07, 2013 at 11:13 AM 0
Share

it is, there is no rotation of any sort though

avatar image $$anonymous$$ · Feb 07, 2013 at 12:08 PM 0
Share

What's your problem then? I copy-pasted your script on a cube, made another cube child of that cube and used that as "currentGun", and it works. You're not telling us if your character does NOT rotate at all, or it rotates but not how you want?!

avatar image ProjectCryken · Feb 07, 2013 at 12:19 PM 0
Share

I JUST said there is no rotation -_-

avatar image $$anonymous$$ · Feb 07, 2013 at 12:24 PM 0
Share

oh right, sorry for trying to get some info.. Place Debug.Log() statements in your code so you see what values are computed or if you even have reference to all the objects you're using. Your problem is probably not with this script, since it works for me. Debugging is your friend.

avatar image ProjectCryken · Feb 07, 2013 at 12:54 PM 0
Share

Ok, so I've just found that newPosition is stuck at 0,0,0 even though position is working fine. Any ideas?

Show more comments
avatar image
0

Answer by OmarAlhaddad · Feb 14, 2013 at 01:10 PM

Dont use LookAt for anything not totally 3D, calculate your rotation angle manually instead.

there are many ways, here is one (might need to tweak it depending on your setup):

 float angle = (Mathf.Atan2 (target.y, target.x) * Mathf.Rad2Deg) -90f;

Goodluck

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

10 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

Related Questions

Cannonball not shooting according to the rotation of the cannon. 1 Answer

How to make half body of a 3d model you imported to follow the mouse position? Similar to a 2.5D shooter Platform game. 1 Answer

How do i make the character look the mouse 0 Answers

Side Scrolling Shooter. How to aim my bullet trajectory at the mouse cursor? 2 Answers

How To Get Object To Aim at Mouse in Top Down 3D Shooter 4 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