• 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 da_player16 · Jul 13, 2014 at 04:35 AM · 2d2d rotation

2D topdown character controller

Hi I'm trying to make a 2D top down game. I have been trying to get my movement script to work but It has some bugs. It uses the WASD keys to move. The problem is when both W and A ( or W and D or D and S or A and S) get pressed I want the character to rotate 45 degrees so he is facing the correct Direction and move forward. I have managed this but there are a few bugs. When the W and A key is pressed the character has three times as much velocity when moving forward. Can any one help me with this?

Heres my code

using UnityEngine; using System.Collections;

public class Movement : MonoBehaviour { //the speed that you want the object to go at public float speed; //this must be 2/3 of speed to even out the speed when two buttons are pressed public float minusSpeed;

 bool isUp = true;
 bool isDown = false;
 bool isRight = false;
 bool isLeft = false;


 void Update () 
 {

     if(isRight == true) {
             transform.eulerAngles = new Vector3(0, 0, -90);
         }

     if(isLeft == true) {
             transform.eulerAngles = new Vector3(0, 0, 90);
         }

     if(isUp == true){
             transform.eulerAngles = new Vector3 (0, 0, 0);
         }

     if(isDown == true) {
             transform.eulerAngles = new Vector3(0, 0, 180);
         }
         
      if(Input.GetKey(KeyCode.D)){
         transform.Translate (Vector2.up * speed);
         isRight = true;
         isLeft = false;
         isUp = false;
         isDown = false;
         }

     if(Input.GetKey(KeyCode.A)){
         transform.Translate (Vector2.up * speed); 
         isLeft = true;
         isRight = false;
         isUp = false;
         isDown = false;
         }

     if(Input.GetKey(KeyCode.W)){
         transform.Translate (Vector2.up * speed);
         isLeft = false;
         isRight = false;
         isUp = true;
         isDown = false;
         }

     if(Input.GetKey(KeyCode.S)){
         transform.Translate (Vector2.up * speed);
         isLeft = false;
         isRight = false;
         isUp = false;
         isDown = true;
         }

     if(Input.GetKey(KeyCode.D)){
         if(Input.GetKey(KeyCode.W)){
             transform.eulerAngles = new Vector3(0, 0, -45);
             transform.Translate (Vector2.up * speed);
             isRight = false;
             isLeft = false;
             isUp = false;
         }
     }

     if(Input.GetKey(KeyCode.D)){
         if(Input.GetKey(KeyCode.S)){
             transform.eulerAngles = new Vector3(0, 0, 225);
             transform.Translate (Vector2.up * speed);
             //Debug.Log(Vector2, speed);
             isRight = false;
             isLeft = false;
             isUp = false;
         }
     }

     if(Input.GetKey(KeyCode.A)){
         if(Input.GetKey(KeyCode.W)){
             transform.eulerAngles = new Vector3(0, 0, 45);
             transform.Translate (Vector2.up * speed);
             isRight = false;
             isLeft = false;
             isUp = false;
         }
     }
     if(Input.GetKey(KeyCode.A)){
         if(Input.GetKey(KeyCode.S)){
             transform.eulerAngles = new Vector3(0, 0, 135);
             transform.Translate (Vector2.up * speed);
             isRight = false;
             isLeft = false;
             isUp = false;
         }
     }
 }

}

Comment
Add comment · Show 1
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 da_player16 · Jul 15, 2014 at 12:39 AM 0
Share

Or do I put it in transform.eulerAngles = new Vector3(0, 0, -90).normalized;?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by cyberlarry · Jul 14, 2014 at 09:31 PM

It is more easy, you must do a animator tutorial.

And add this code

 using UnityEngine;
 using System.Collections;
  
 public class Player : MonoBehaviour {
  
 private Animator anim;
 public Vector3 startPosition;
 public Vector2 speed = new Vector2(1,0);
  
 void Start(){
  
 startPosition = transform.position;
 anim = GetComponent ();
 }
  
 void Update () {
  
  
 float inputX = Input.GetAxis("Horizontal");
 float inputY = Input.GetAxis ("Vertical");
  
 anim.SetFloat("SpeedX",inputX);
 anim.SetFloat("SpeedY",inputY);
  
 Vector3 movement = new Vector3
 (speed.x*inputX, speed.y*inputY,0);
  
 movement*= Time.deltaTime;
 transform.Translate(movement);
 }
  
 void FixedUpdate() {
 float lastinputX = Input.GetAxis("Horizontal");
 float lastinputY = Input.GetAxis("Vertical");
  
 if (lastinputX !=0 || lastinputY !=0){
 anim.SetBool("walking",true);
  
  
 if (lastinputX>0){
 anim.SetFloat("LastMoveX",1f);
 }
 else if (lastinputX<0){
 anim.SetFloat("LastMoveX",-1f);
 }else {
 anim.SetFloat("LastMoveX",0f);
 }
  
  
 if (lastinputY>0){
 anim.SetFloat("LastMoveY",1f);
 }
 else if (lastinputY<0){
 anim.SetFloat("LastMoveY",-1f);
  
 }else {
 anim.SetFloat("LastMoveY",0f);
 }
  
 }else {
 anim.SetBool("walking",false);
 }
 }
 }
 


Comment
Add comment · Show 1 · 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 theRelation · Oct 03, 2014 at 04:22 PM 0
Share

thanks so much for this! i've been trying to apply this to npc follow animations and i've run into all sorts of trouble. the npc has four walking animations for four directions (ins$$anonymous$$d of 8) and slerps within range of gameobjects on the player layer. yet for some reason:

1) walking animations only transition on one axis 2) transition seems to be based on world space rather than local space

void Update () {

     float lastSpeedX = 0;
     float lastSpeedZ = 0;
     float speedX =  Craycray.InverseTransformDirection(rigidbody.velocity).x /Time.deltaTime;
     float speedZ =  Craycray.InverseTransformDirection(rigidbody.velocity).z / Time.deltaTime;

     if(lastSpeedX != speedX ){
         
         if (speedX < 0.5f )
         {
             anim.SetInteger ("SpeedX", 1);
             anim.SetInteger ("SpeedZ", 0 );

         }
         else if (speedX > 0.5f)
         {
             anim.SetInteger ("SpeedX", -1);
             anim.SetInteger ("SpeedZ", 0);
         
         }
         else
         {
             anim.SetInteger ("SpeedX", 0);
         }
     }
     if (lastSpeedZ != speedZ){
         if (speedZ < 0.5f )
         {
             anim.SetInteger ("SpeedZ", 1);
             anim.SetInteger ("SpeedX", 0);
         }
         else if (speedX > 0.5f)
         {
             anim.SetInteger ("SpeedZ", -1);
             anim.SetInteger ("SpeedX", 0);
         }
         else
         {
             anim.SetInteger ("SpeedZ", 0);
         }

     lastSpeedX = speedX;
     lastSpeedZ = speedZ;
     


}}

avatar image
0

Answer by RedHillbilly · Jul 13, 2014 at 09:31 AM

You shouldn't use transform.translate to move your charachter. This basically is the same as a teleportation.

Use transform.velocity=new vector2(...);

For your magnitude problem: you are giving x and y the same magnitude when moving in diagonal as when moving along the axis.

Use new vector2(...).normalized to have a normal magnitude in every direction (square root of the squared sum of the elements is always equal to one)

Cheers

Comment
Add comment · Show 1 · 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 da_player16 · Jul 13, 2014 at 10:56 PM 0
Share

Should I use normalized for all of the magnitude calculations? or just for the ones where I am asking for two keys as input?

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

Look for the player isn't working 1 Answer

Need help to convert this raycasting 3d Script into a 2D x&y version 0 Answers

2d rotation doesn't work. 0 Answers

How to make a 2D autoaim,How to make an autoaim for a 2D game. 0 Answers

2D rotate 90 degrees issue 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