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 MaZy · Jul 11, 2014 at 09:18 PM · directionangletopdownshooterwasd

2D TOP DOWN - W A S D as direction

Hello I need here really a help

I am making a top down 2d tank shooter game. The direction should based on keys WASD. So if I want to go right the Z rotiation should be zero. If I go to left so it should be 270. So I don't know really how to calculate it. Because the Quaternion gives me headache.

When I used old engine it was without Quaternion. I just made Direction.ToAngle() and I got the angle were I could use .setAngle(angle). For smooth I just did SmoothAng = currentAng - (angle - CurrentAng) *0.1f (it hadn't lerp) In Unity it needs to Vectors to get an angle which confusing me while in old engine I used just one Vector like Quaternion.Euler.

Quaternion seems more complex. There is LookRotation or something else. Don't know really how these do the culculation. So I hope you can help me :S

Important!! It needs to be a smooth rotation.

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 robertbu · Jul 11, 2014 at 09:20 PM

While this can easily be done with Quaternions, it also can be easily done just using eulerAngles. Given you are rotating around 'y' axis, you can do something like this:

 #pragma strict
 
 var speed = 3.0;
 private var angleTo = 0.0;
 private var angle = 0.0;
  
 function Update() {
     if (Input.GetKeyDown(KeyCode.A))
         angleTo = -90.0;
     if (Input.GetKeyDown(KeyCode.S))
         angleTo = 180.0;
     if (Input.GetKeyDown(KeyCode.D)) 
         angleTo = 90.0;
     if (Input.GetKeyDown(KeyCode.W))
         angleTo = 0.0;
         
     angle = Mathf.Lerp(angle, angleTo, Time.deltaTime * speed);
     transform.eulerAngles = Vector3(0.0, angle, 0.0);
 }

A couple things to note here. First, I never read from eulerAngles. I keep my own variable (angle) and just write to them. Second, I'm using Time.deltaTime instead of 0.1. Since the time of each frame varies, you cannot used a fixed value in Update(). You could use a fixed value if you wanted to use FixedUpdate().

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
avatar image
0

Answer by IKDev · Aug 24, 2020 at 07:20 PM

Well, I think this is solution (sorry because it is in C#):

 rb.MovePosition(rb.position + new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")) * [speed of character, for example 5] * Time.fixedDeltaTime);
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Setting rotation angle of a bullet fired towards the mouse 1 Answer

Orthographic TopDown game Shooting Direction 1 Answer

Aiming direction altered after collision 1 Answer

How do I make a laser pointer? 2 Answers

How can I dash in a specific direction 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