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
0
Question by Isstanger · Apr 29, 2020 at 08:19 PM · scripting problemscripting beginnermousepositioncamera viewport

Issue with ScreenToWorldPoint

So, ive been trying to set up a tank game with a top down camera view (the game is still 3D). I want to write a script to let me point the turret towards the mouse. The camera has a script to keep folowing the tank but it is not attached to the tank gameObject itself

I am also not sure whether the script should be attached to the turret or the camera

the compiler keeps sending the same Error

Assets\Scripts\TowerControl.cs(8,12): error CS0246: The type or namespace name 'cam_Main' could not be found (are you missing a using directive or an assembly reference?)

this is my script so far, I haven't had much luck though :/

using UnityEngine;

// continuously point turret at mouse

public class TowerControl : MonoBehaviour {

 public cam_Main.ScreenToWorldPoint target;

 void Update()
 {
     //Rotate the turret every frame so it keeps looking at the target
     TankFree_Tower.transform.LookAt(target);

     // Same as above, but setting the worldUp parameter to Vector3.left turns the camera on its side
     TankFree_Tower.transform.LookAt(target, Vector3.left);
 }

}

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
0
Best Answer

Answer by Luis_Gan · Apr 29, 2020 at 08:50 PM

Something like this? (Attach the script to the tower) alt text

 public Camera mainCamera;
     void Update()
     {
         Vector3 position = mainCamera.ScreenToViewportPoint(Input.mousePosition);
         transform.LookAt(position);
     }



look.gif (220.1 kB)
Comment
Add comment · Show 9 · 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 Isstanger · Apr 29, 2020 at 08:58 PM 0
Share

that wont work though, because you would have to put the main camera variable on the tower, and the tower is not a camera (see error) \/

UnassignedReferenceException: The variable mainCamera of TowerControl has not been assigned. You probably need to assign the mainCamera variable of the TowerControl script in the inspector.

avatar image Luis_Gan Isstanger · Apr 29, 2020 at 09:05 PM 0
Share

Set the camera tag to $$anonymous$$ainCamera and it will work.

     private Camera mainCamera;
 
     void Start()
     {
         mainCamera = Camera.main;
     }
 
     void Update()
     {
         Vector3 position = mainCamera.ScreenToViewportPoint(Input.mousePosition);
         transform.LookAt(position, Vector3.up);
     }

avatar image Isstanger Luis_Gan · Apr 29, 2020 at 10:52 PM 0
Share

Wait, i'm sorry, something got jacked up. if you could please reply your latest comment to this message, something got messed up

Show more comments
avatar image Fuzzel_ · Jun 01, 2020 at 09:46 PM 0
Share
 mainCamera.ScreenToViewportPoint(Input.mousePosition);
 transform.LookAt(position);

This will not behave correctly. ScreenToViewportPoint converts simply a mouse position between (0,0) and (Screen.width, Screen.height) to the range of (0,0) - (1,1)

If you then just use this as the argument for transform.LookAt() the object will always rotate to look at world positions between (0,0,0) and (1,1,0), and not around the tank. At the very least you actually need to transform the mouse position into a range of (-1,-1) to (1,1) to properly offer all 360° directions, as (0,0) - (1,1) will only provide the top-right 90° corner of possible direction vectors.

avatar image
0

Answer by Isstanger · Apr 29, 2020 at 08:30 PM

This is hurting my brain, way more than I thought it would

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 Fuzzel_ · Jun 01, 2020 at 09:32 PM

If you want to have the tank look at your mouse, with a top-down perspective, I assume you want to the tank to rotate only on the y-axis towards the mouse, and that the camera is looking directly down onto the world with a 90-degree angle.

Given that the camera is directly looking down onto the tank, and is always centred on the tank, you can simply take the mouse position: Mouse.current.position.ReadValues() / Input.mousePosition (depending on if you are using the new input system or not) and subtract the centre screen position: new Vector2(Screen.width/2, Screen.height/2)

With this direction vector, you can either create a new rotation with Quaternion.LookRotation(), or use transform.LookAt()

 public class LookAtMouse : MonoBehaviour
 {
     private readonly Vector2 screenCenter = new Vector2(Screen.width/2.0f, Screen.height/2.0f);
 
     private void Update()
     {
         Vector2 mousePosition = Mouse.current.position.ReadValue(); // Input.mousePosition in the old input system
         Vector2 directionToMouse = (mousePosition - screenCenter).normalized;
 
         transform.rotation = Quaternion.LookRotation(new Vector3(directionToMouse.x, 0, directionToMouse.y), Vector3.up);
         // - or -
         transform.LookAt(transform.position + new Vector3(directionToMouse.x, 0, directionToMouse.y));
     }
 }

alt text


If you want to have a camera that may be at an angle, the solution becomes a bit more involved because you want to calculate the position of the mouse in your world, or rather, you want to calculate the world position of the point your mouse points at.

This can be done with a raycast, which would also support 3d models that are sticking out from a flat surface, but a faster approach that only considers the flat "floor" can be achieved by calculating a vector from your camera into the world to where you mouse is pointing at and then calculating the intersection with the "floor plane", to get the world position of the point your mouse is pointing at. With this you can either do transform.LookAt() or, similar to the code above, subtract the tanks world position to get the direction vector from your tank to the world position of the point your mouse is pointing at and do Quaternion.LookRotation().


If you are interested in that because the camera might not always be centred on the tank or might not always look down at the tank at a 90-degree angle, I'll happy to provide you with some code about the more complex solution I just described.


2020-06-01-23-35-34.gif (249.8 kB)
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

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

Related Questions

How do you check if a game object is set active using an if statement? 2 Answers

Problems with Gyroscope VR Car Game 0 Answers

Array GameObjects Destinations NavMeshAgent 2 Answers

loop vs if spell casting script 1 Answer

Object not set to a instance of a object 1 Answer


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