• 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 tanagno · Oct 19, 2015 at 11:35 PM · 2dmovementrotate objectmouseposition

Making the position of the mouse cursor, the reference point for forward movement (2D)

Hello

Complete newbie here so I would be very grateful for any help.

I am trying to make an overhead shooter thing (similar to Hotline Miami in terms of style) where the player will can be moved forward, depending on which way they are facing.

So I managed to work out how to move a gameobject in 2d, using getkey. Now a block will move, using the WASD keys. W is up, D is down, A for left, D for right

I managed to work out how to get the gameobject to rotate towards the mouse position.

However, when I put them together, it doesn't seem to work how I want. It moves just like Hotline Miami but I want something slightly different. I want to make it so that wherever the mouse cursor is, by pressing W, I want the character to move forwards towards it and not move up the screen.

How do I make it so the gameobject will move forward towards the mouse cursor (and back and left and right) in relation to its current rotation.

Here is the code for movement:

var speed : int = 2;

function Start () {

}

function Update () {

 if (Input.GetKey ("d")){
     transform.position += Vector3(speed * Time.deltaTime, 0, 0); }
     if (Input.GetKey ("a")){
     transform.position -= Vector3(speed * Time.deltaTime, 0, 0); }
     if (Input.GetKey ("w")){
     transform.position += Vector3(0, speed * Time.deltaTime, 0); }
     if (Input.GetKey ("s")){
     transform.position -= Vector3(0, speed * Time.deltaTime,  0); }

}

Here is the code for rotation:

function Update ()

 {
     var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
     var rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);
     
     transform.rotation = rot;
     transform.eulerAngles = Vector3 (0, 0, transform.eulerAngles.z);
     GetComponent.<Rigidbody2D>().angularVelocity = 0;
 }

Hope that makes sense. Thank you in advance

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
0

Answer by AshleyJamesy · Oct 25, 2015 at 04:54 PM

 if (Input.GetKey("d"))
         {
             transform.position += Vector3.right * speed * Time.fixedDeltaTime;
         }
         if (Input.GetKey("a"))
         {
             transform.position -= Vector3.right * speed * Time.fixedDeltaTime;
         }
         if (Input.GetKey("w"))
         {
             transform.position += Vector3.forward * speed * Time.fixedDeltaTime;
         }
         if (Input.GetKey("s"))
         {
             transform.position -= Vector3.forward * speed * Time.fixedDeltaTime;
         }

         //Rotation
         Vector3 m_TankPos = Camera.main.WorldToScreenPoint(transform.position);
         Vector3 m_MouseDirection = Input.mousePosition;
         m_MouseDirection.x = m_MouseDirection.x - m_TankPos.x;
         m_MouseDirection.y = m_MouseDirection.y - m_TankPos.y;
         m_MouseDirection.Normalize();
 
         float m_Angle = Mathf.Atan2(-m_MouseDirection.y, m_MouseDirection.x) * Mathf.Rad2Deg;
 
         GetComponent<Rigidbody>().MoveRotation(Quaternion.Euler(new Vector3(0, m_Angle - 270, 0)));
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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

ScreenToWorldPoint question 1 Answer

How to move 2D character from one place to another by "movement" and not "teleporting"? 2 Answers

My 2D Player is slightly being dragged sometimes after firing. 1 Answer

2d character, how to limit rotation to z, with character looking left? 0 Answers

Megaman Movement 0 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