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 John509 · Apr 04, 2017 at 05:16 AM · c#unity 5rotationscripting problemlookat

I need help writing a simple script for making an object face the mouse in 2D.

I've spent the last 3 days trying to get a simple sprite to face the mouse wherever I put it on the screen, and so far I've crashed my computer twice and have gone to bed angry three times, so I finally thought I needed to ask the forums about this. I'm using C#, in monodevelop, and have so far scripted the object to move around, and have put in placeholder entries for some future items, but I need the object to face it's front side toward my mouse's location on the screen every frame. I'm rotating around the z axis, y is vertical, x is horizontal. I'm sure this is extremely simple to complete, but I've scoured the internet to no avail, so any help is welcome.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LRmovement : MonoBehaviour {
     public int HP;
     public GameObject Controller;
     public GameObject Cannon;
     public float Speed;
     public Vector3 mousePosition;
     public float targetDistance;
 
     void Start () 
 
     {
         
     }
 
     void Update () 
 
     {
         if (Input.GetKey (KeyCode.W))
             transform.position += Vector3.up * Speed * Time.deltaTime;
         if (Input.GetKey (KeyCode.A))
             transform.position += Vector3.left * Speed * Time.deltaTime;
         if (Input.GetKey (KeyCode.S))
             transform.position += Vector3.down * Speed * Time.deltaTime;
         if (Input.GetKey (KeyCode.D))
             transform.position += Vector3.right * Speed * Time.deltaTime;
         
         mousePosition = Input.mousePosition;
     }
 
     void LateUpdate ()
 
     {
         mousePosition = Camera.main.ScreenToWorldPoint (mousePosition);
         mousePosition.z = targetDistance;
     }
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by VeeooshL · Apr 04, 2017 at 08:22 AM

this should work - place it inside your update:

         mousePosition = Input.mousePosition;            
         mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
  
         Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward );
         transform.rotation = rot;   
         transform.eulerAngles = new Vector3(0, 0,transform.eulerAngles.z); 

I didn't write this myself, I found it here, the second post down:

https://forum.unity3d.com/threads/2d-sprite-look-at-mouse.211601/

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 John509 · Apr 07, 2017 at 02:42 PM 0
Share

This is just giving me the same problem I've been having, it puts the mouse position as the pixel coordinates on the screen, not the game world coordinates. @VeeooshL

avatar image
0

Answer by yade_123 · Jun 03, 2018 at 03:23 PM

This question was asked more than a year ago still I will upload this answer for anyone facing similar problem . I was able to put together this script for a top down 3d game it should work the same for 2d games in the x-z plane

public Vector3 mousePos,lookPos; void Update () { mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10); lookPos = Camera.main.ScreenToWorldPoint(mousePos); lookPos = transform.position-lookPos; float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.AngleAxis(angle, Vector3.down); }

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

8 People are following this question.

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

Related Questions

Temporarily turn off the relative rotation from parent in my script while keeping the relative position updated? 1 Answer

Flip over an object (smooth transition) 3 Answers

How to check if animator is in crossfade 1 Answer

Creating a GameObject variable without instantiating it? 1 Answer

Why there is an 'SerializationException: serializationStream supports seeking, but its length is 0' error when I click the load button? 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