Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Tageos · Jul 09, 2015 at 05:34 PM · c#2drotationmovement

Make movement of an object independant.

Hello!

I am making a top down 2d game. My character has currently a movement script and a rotation script (i like to keep things separated). The movment script is simple; press W to go up, A to go left, S to go down and D to go right. The rotation script makes the character always face the mouse cursor.

The problem is that when in gamemode the character moves right if he is facing right when i press W, he moves left if he is facing left when i press W.

He moves where he is facing when i want him to go up. This applies for every direction. He needs to look at the direction he is going for the controls to make sence. How could i "seperate" the movement from the rotation?

This is the movement script by the way:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public float speed;    
 
     void Update () {
         Movement ();
     }    
 
     void Movement()
     {
         if (Input.GetKey (KeyCode.D)) {
             transform.Translate (Vector2.right * speed * Time.deltaTime);
         }
         if (Input.GetKey (KeyCode.W)) {
             transform.Translate (Vector2.up * speed * Time.deltaTime);
         }
         if (Input.GetKey (KeyCode.A)) {
             transform.Translate (-Vector2.right * speed * Time.deltaTime);
         }
         if (Input.GetKey (KeyCode.S)) {
             transform.Translate (-Vector2.up * speed * Time.deltaTime);
         }
         if ((Input.GetKey (KeyCode.A) && Input.GetKey (KeyCode.W))
             ||(Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.D))
             ||(Input.GetKey (KeyCode.D) && Input.GetKey (KeyCode.S))
             ||(Input.GetKey (KeyCode.S) && Input.GetKey (KeyCode.A))) 
         {    
             speed = 0.85f;
         } else {
             speed = 1.2f;
         }
     }
                         
 }

This is the rotation script:

 using UnityEngine;
 using System.Collections;
 
 public class LookAtMouse : MonoBehaviour {
 
     void Update () {
         //rotation
         Vector3 mousePos = Input.mousePosition;
         mousePos.z = 5.23f;
         
         Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
         mousePos.x = mousePos.x - objectPos.x;
         mousePos.y = mousePos.y - objectPos.y;
         
         float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle-90));
     }
 }

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

Answer by Tageos · Jul 09, 2015 at 05:42 PM

Nvm i think i found a solution. I just made a new gameobject named "charlegs" (lol) and attached the movementscript to it, i then made the charcter a child of the charlegs :)

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

21 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

Related Questions

How can i make a sprite move in the direction it is rotated? 1 Answer

Flip over an object (smooth transition) 3 Answers

problem with twin stick shooter controls 2 Answers

Sprite will not roate when arrow keys are pressed 2 Answers

Making a bubble level (not a game but work tool) 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