Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 isagard · Aug 23, 2016 at 07:40 AM · 2drotationmovementtopdownspace shooter

How do I make a 2D object face the direction it is moving in top down space shooter

I am very new to unity and have no clue how to make this work, I've been working on it for hours. Here is what I have so far:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityStandardAssets.CrossPlatformInput;
 
 public class PlayerController : MonoBehaviour {
 
     private Rigidbody2D body;
     public float speed;
     private int count;
     public Text counttext;
     public Text WinText;
 
 
 
     void Start()
     {
         body = GetComponent<Rigidbody2D> ();
         count = 0;
         WinText.text = "";
         SetCountText ();
 
     }
 
 
     void FixedUpdate()
     {
         
         Vector2 moveVec = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"),CrossPlatformInputManager.GetAxis("Vertical")) * speed;
         body.AddForce (moveVec);
 
 
     }
 
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
3

Answer by villevli · Aug 23, 2016 at 09:15 AM

Add this to your FixedUpdate:

 void FixedUpdate()
 {
     Vector2 moveVec = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"), CrossPlatformInputManager.GetAxis("Vertical")) * speed;
     body.AddForce (moveVec);
     
     if (moveVec != Vector2.zero) {
         transform.rotation = Quaternion.LookRotation(moveVec, Vector3.back);
     }
 }

Vector3.back is likely your upwards direction (towards the camera). If not, then change that accordingly.

If you want smoother rotation, use:

 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveVec, Vector3.back), Time.fixedDeltaTime * turnSpeed);
Comment
Add comment · Show 5 · 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 isagard · Aug 23, 2016 at 04:18 PM 0
Share

The ship rotates on the y and x axis but not the z. I need it to rotate on only the z. As it is now it just disappears.

avatar image villevli isagard · Aug 23, 2016 at 04:48 PM 0
Share

Try:

 transform.rotation = Quaternion.LookRotation(Vector3.forward, moveVec)

The code I posted assumes that the player object's forward axis (blue) points to the direction the player is looking. When using sprites in 2D game, by default it points directly away from the camera when it should point either up or right (x or y) in this top down shooter game. Using that alternative may fix this. (the green axis points where the player is moving / is the forward direction)

avatar image starrtennis villevli · Jan 28, 2019 at 02:07 AM 0
Share

@villevli Now the top of my character (ant) faces forward, ins$$anonymous$$d of his front facing forward.

avatar image AibNihan · Sep 27, 2017 at 12:35 PM 0
Share

Thank you so much @villevli You are the great :)

avatar image starrtennis · Jan 28, 2019 at 01:57 AM 0
Share
 private Vector3 calculateVelocity(){
         return antPos.position - previousPos;
     }
 
     void updateDirection(){
         //Debug.Log(antPos.rotation.ToString ());
         //Debug.Log ("Velocity: " + velocity);
         //antPos.up = velocity;
         Vector3 velocity = calculateVelocity();
         Debug.Log("calculated velocity: " + velocity);
         antPos.rotation = Quaternion.LookRotation (velocity, Vector3.back);
     }


$$anonymous$$y character (ant) disappears when it is moving now.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Top down 2d movement without being affected by rotation 2 Answers

Top-Down Movement in Unity 1 Answer

Top-down movement in 2D 1 Answer

Unity 2D: How can i get my sprite to rotate to the direction I am going? 1 Answer

Add acceleration and deceleration for top down 2D movement. 2 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