• 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 Hamesh81 · Jan 24, 2015 at 03:29 AM · cameraslerpscreentoworldpointaim

How to prevent continuous spinning while aiming at the screen's center?

I am developing a simple 3rd person weapon system. A target gameobject is positioned in the center of the screen using ScreenToWorldPoint on the current camera. The character rotates towards this target via a Quaternion.Slerp when the aim button is held down. The problem is that because the camera is following the rotation of the character and the target positions itself in the center based on the camera it creates an infinity spinning effect; whereby the character rotates to face the target, which rotates the camera following the character, which moves the target since it is not in the center of the camera's screen anymore! How should I resolve this?

The second part of my question is a similar case. While aiming I would like to enable a "Mouse Look" component so that the player is able to "free aim" by moving the mouse. Since this rotates the entire character, it of course causes the same spinning effect as described above since the camera and target try to adjust to the characters rotation causing infinite rotation. What would be the best way to approach this?

Comment
Add comment · Show 4
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 SnStarr · Jan 24, 2015 at 04:06 AM 0
Share

Perhaps not making the Camera a child of the player would help.

avatar image Hamesh81 · Jan 24, 2015 at 04:26 AM 0
Share

The camera isn't actually a child as it is not parented to anything. It is using the smooth follow script to follow the character around.

avatar image SnStarr · Jan 24, 2015 at 04:49 AM 0
Share

Why is the target positioning itself based on the camera? Why does the target move at all unless its trying to attack the player? What do you mean by A target gameobject is positioned in the center of the screen using ScreenToWorldPoint on the current camera? Why are you implementing it that way? I've done some 3rd person controls, attack systems, enemy AI, never had this problem. Unless I am completely misunderstanding what you mean.

avatar image Hamesh81 · Jan 24, 2015 at 06:18 AM 0
Share

The target gameobject is where the character will be ai$$anonymous$$g towards, it is basically a visual representation of the center of the screen (like a reticule). I use the following line to position the target in the center of the screen:

transform.position = currentCamera.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.height/2, distance));

This is obviously camera dependent, since the position and rotation of the camera will change what the center of the screen is. In other words, if the camera is rotated 90 degrees on the y axis, the position of the center of the screen will be different to if the camera was rotated 90 degrees on the x axis. Of course if there's a better way to do the above I am open to suggestions :)

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Need explanation for this code 1 Answer

Need help putting an object at a point based on screen coordinates 1 Answer

mouse position on terrain 1 Answer

Indicate with markers the direction of Gameobjects which are outside of the camerafield. 0 Answers

Orthographic camera with rotation and ScreenToWorldPoint 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