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 SirWalrusII · Jul 22, 2014 at 03:18 PM · 2d2d rotation

2D Top Down, Making Player rotate & move with mouse?

I am working on an all 2D top-down stealth game, and i would like for the player to rotate when i move the mouse. I have half of this done. Right now the player turns when the mouse moves, but the player doesn't move in the direction they're facing. i would like that to happen.

Here is the script i'm using, it is attached to the player,

 using UnityEngine; 
 
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
     public int playerSpeed = 5;
     // Update is called once per frame
     void Update () {
         
         transform.position += Vector3.right * Input.GetAxis ("Horizontal") * playerSpeed * Time.deltaTime;
         transform.position += Vector3.up * Input.GetAxis("Vertical") * playerSpeed * Time.deltaTime;
         
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
         transform.LookAt(ray.GetPoint(0),Vector3.forward);
         
         Camera.main.transform.position = new Vector3 (transform.position.x, transform.position.y, Camera.main.transform.position.z);
         
         
     }
 }

Sorry if that question was a bit confusing to any. Also, i am trying hard to learn how to script, most of my beginners knowledge being in Javascript.

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 · Jul 22, 2014 at 05:06 PM

Answering a question like this one always involves figure out how you've setup your scene. From this code I assume:

  • The camera is looking towards positive 'z' making your 2D action on the XY plane

  • The front of your player is the positive 'z' side.

  • You are using an Orthographic camera.

  • You don't indicate what controls forward movement, so I'll use 'Input.GetAxis("Vertical")'.

Note if you consider the positive 'z' side of your object the front, then 'transform.forward' will be the direction your object is facing regardless of the way it is facing. So one way to move forward would be:

 transform.position * Time.deltaTime * playerSpeed * Input.GetAxis("Vertical") * transform.Forward;

Another way is to use Transform.Translate:

  transform.Translate(0.0f, 0.0f, Time.deltaTime * playerSpeed * Input.GetAxis("Vertical);

You will need to remove lines 10 and 11. Also playerSpeed is better as a float rather than an int.

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 Bojidar · Jul 22, 2014 at 05:14 PM 0
Share

I would also suggest to put all the code in "FixedUpdate" and remove deltaTime.

avatar image robertbu · Jul 22, 2014 at 05:54 PM 0
Share

@Bojidar - why? FixedUpdate() generally runs at a slower frame rate than Update(). Generally code dealing with Physics is moved into FixedUpdate() due to physics calculations.

avatar image SirWalrusII · Jul 22, 2014 at 08:16 PM 0
Share

Oh yeah, probably should have mentioned that stuff.

  • The camera is towards the positive Z axis

  • $$anonymous$$y character is the attached image, so i assume the Front is actually positive Y, the bottom of his feet being positive Z

  • I am using an Orthographic camera

  • WASD for controlling the player, I want the mouse to control what direction is facing/moves towards.

    Ex. moving the mouse Northwest, the player rotates to the Northwest,Pressing "W" then results in moving forward,well, Northwest

alt text

topdownplayer02copy.png (5.3 kB)
avatar image robertbu · Jul 22, 2014 at 09:34 PM 0
Share

The code solutions I outlined should work for this setup. 'W' and 'S' are the positive and negative keys for the "Vertical" axis. Note you have a problem here:

 transform.LookAt(ray.GetPoint(0),Vector3.forward);

The second parameter to LookAt() is the preferred axis of rotation. For the setup you describe, I believe you want Vector3.up for that parameter.

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

2 People are following this question.

avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Having problems with 2d collision triggers (javascript) 1 Answer

GetComponent().enabled = true; 1 Answer

Help setting maxAceleration 0 Answers

Lifting box 2D C# 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