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
1
Question by SPIGS · Dec 15, 2013 at 12:14 AM · spritecursortopdownlook

How do I get my character to look in the direction of the cursor?

I'm make a top-down RPG and I wanted my character to be able to "look" around using the cursor. Here is what I mean:

alt text

I want to do it this way so I can implement projectile weapons such as a bow and arrow and spells such as fireball, but I don't know how to approach this. Answers appreciated.

diagram.png (6.5 kB)
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

1 Reply

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

Answer by robertbu · Dec 15, 2013 at 09:26 AM

How you implement this will depend on how you implement your character. You have many choices:

  • Use a Quad and an array of textures

  • Use a Quad and a sprite sheet. Manipulate the material offset and scale.

  • Use a Quad and a texture atlas and manipulate the uv coordinates of the quad.

  • Use Unity's new Sprite

  • Use a third-party tool like NGUI or EZGUI

Regardless of your solution, a likely first step is translate the angle into an integer that can be used to select a texture from an array, or set a frame. Here is a bit of code that makes that translation:

 #pragma strict
  
  function Update() {
      var screenPos = Camera.main.WorldToScreenPoint(transform.position);
      var direction = Input.mousePosition - screenPos;
      var angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
      if (angle < 0.0)
          angle = 360.0 + angle;
      
      angle += 22.5;
      
      var i : int = (angle / 45.0);
      i = i % 8;
      
      Debug.Log(i);
 }

'i' will be 0 when the cursor is within 22.5 degrees of directly to the right, and the integers increase counter clockwise. To test, put this script on a game object, run, and move the cursor around the object.

If you have an array of textures representing the 8 possible directions, you would use this code like:

 renderer.material.mainTexture = textures[i];
Comment
Add comment · Show 4 · 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 SPIGS · Dec 15, 2013 at 11:39 PM 0
Share

Thanks I'll give this a go, and translate it into C#. Thanks

avatar image SPIGS · Dec 16, 2013 at 12:10 AM 0
Share

Well I tried to translate it. Turns out "angle" has to be a float. Ok, I tried converting it to and int and it didn't work. Do you $$anonymous$$d rewriting this in C#?

avatar image robertbu · Dec 16, 2013 at 12:49 AM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class Facing : $$anonymous$$onoBehaviour {
 
     void Update() {
         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
         Vector3 direction = Input.mousePosition - screenPos;
         float  angle = $$anonymous$$athf.Atan2(direction.y, direction.x) * $$anonymous$$athf.Rad2Deg;
         if (angle < 0.0f)
             angle = 360.0f + angle;
         
         angle += 22.5f;
         
         int  i = (int)(angle / 45.0f);
         i = i % 8;
         
         Debug.Log(i);
     }
 }
avatar image SPIGS · Dec 16, 2013 at 01:54 AM 0
Share

Thanks again.

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

16 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

Related Questions

How do i change sprite regarding to position 1 Answer

2D lock x,y rotation to look at target 2 Answers

My sprites wont change? 0 Answers

unity 2D help 0 Answers

2D character movement is jittery (top-down) 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