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 janbpunkt · Sep 02, 2014 at 10:10 PM · mousepositionworldalternative

Alternative to Camera.main.ScreenToWorldPoint

hi community :)

i'm absolutely new to Unity3d and c# as well.

currently i'm trying to create a little top down view game and run into one big issue, which i cannot solve by myself.

first i tried to move around a character (sprite) with WASD. succeeded.

then i added some kind of a "look at mouse" script. that's working, too.

now, i want my camera to follow the character. fails!

i thought, i could easily make the mainCamera as a child of the sprite in the hierachy, but that causes my sprite to rotate around its z-axis constantly.

my guess is, that this results from the way, how i calculate the new rotation of the sprite.

 using UnityEngine;
 using System.Collections;
 
 public class LookAtMouse : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         
         transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
         Debug.Log ( Input.mousePosition + " vs " + mousePos);
     }
 }
 

yes, that's where i'm stuck now. i think, there's a way to solve it, if i could only figure out how. maybe you could calculate the mousepointer's position (in the world) using another way?

Thanks!

Comment
Add comment · Show 2
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 · Sep 03, 2014 at 05:38 AM 0
Share

I'm baffled by your question.

  • There is no reference to the character in this code, so how could it follow a character?

  • ScreenToWorldPoing() is used incorrectly for a perspective camera and will only return the position of the camera.

  • There's no code here that modifies the transform.position, so the camera will not move.

  • The rotation code always looks towards the world 'z'

I think you need to explain further exactly what you are trying to do, and how you have things setup.

avatar image janbpunkt · Sep 03, 2014 at 09:46 AM 0
Share

the code above is for the rotation of the sprite. it's attached to the sprite. the camera should not rotate or do anything else. it should just follow the sprite, looking down on it, but it starts rotating as soon as i made it a child of the sprite.

1 Reply

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

Answer by froYo · Sep 03, 2014 at 11:35 AM

Create a new script and add this.

 public Transform playerTransform;
 
     void Update ()
     {
         transform.position = new Vector3(playerTransform.position.x, playerTransform.y, transform.position.z);
     }

Drag this script onto your camera and drag the player into the playerTransform variable in the editor.

For smoother movement, consider using:

 Vector3.Lerp


Hope this helps.

Comment
Add comment · Show 1 · 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 janbpunkt · Sep 03, 2014 at 11:41 AM 0
Share

That solved my problem! Thank you very much.

I really need to learn how to think in Unity -.-

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Dynamic world in Unity 4 Answers

iTween MoveAdd world coordinates Simple 2 Answers

Do I need a license when using models of real world things (cars, buildings etc)? 2 Answers

How to move Character along world floor? 2 Answers

Convert world space to GUI Rect? 5 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