• 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 VAN-D00M · Jul 11, 2014 at 09:08 PM · rotationmovementtop down shooter

Player won't look in direction and travel at the same time?

Hi

I'm doing a top down game and would like my player to look in the direction he is going in. I have found some code that changes the players rotation w$$anonymous$$ch to me looks like it should move the player. I have also got some code that will move the player backwards and forwards, left and right but I can't merge the two different section of code together w$$anonymous$$ch makes me t$$anonymous$$nk there must be another way and after looking at everyoones else's questions and solutions, I am stumped.

T$$anonymous$$s changes my rotation:

void Update() { if (Input.GetKeyDown (KeyCode.W)) transform.forward = new Vector3 (0f, 0f, 1f) * speed * Time.deltaTime; else if (Input.GetKeyDown(KeyCode.S)) transform.forward = new Vector3(0f, 0f, -1f); else if (Input.GetKeyDown(KeyCode.D)) transform.forward = new Vector3(1f, 0f, 0f); else if (Input.GetKeyDown(KeyCode.A)) transform.forward = new Vector3(-1f, 0f, 0f); } and t$$anonymous$$s allows me to move:

 if (Input.GetKey (KeyCode.UpArrow)) 
         {
             transform.Translate(Vector3.forward * speed * Time.deltaTime);
         }
         
         if (Input.GetKey (KeyCode.DownArrow)) 
         {
             transform.Translate(Vector3.forward * -speed * Time.deltaTime);
         }
         
         if (Input.GetKey (KeyCode.LeftArrow)) 
         {
             transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
         }
         
         
         if (Input.GetKey (KeyCode.RightArrow)) 
         {
             transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
         }

Two different pieces of code w$$anonymous$$ch both do jobs I would like to do. They won't work together or even be in the same script as each other.

I have tried

 transform.forward = new Vector3 (0f, 0f, 1f) += transform.Translate(Vector3.forward * -speed * Time.deltaTime;

I have also tried

 transform.position += person.transform.forward * Time.deltaTime * speed;

but it won't have it. I t$$anonymous$$nk i'm close but I am out of ideas.

Please help if you can.

Harry

PS I know the inputs are different, its not like that in my code. I back my code up as I create and find it, t$$anonymous$$s is the back up w$$anonymous$$ch hasn't been changed to match. :)

EDIT:

Why won't t$$anonymous$$s work?

 private Vector3 direction; //somet$$anonymous$$ng like t$$anonymous$$s??
 
 void Update()
 {
       if (Input.GetKey (KeyCode.UpArrow)) 
         {
             direction = transform.Translate(Vector3.forward * speed * Time.deltaTime);
             transform.rotation = Quaternion.LookRotation(direction);
                 }
 }

I know t$$anonymous$$s moves the player transform.Translate(Vector3.forward speed Time.deltaTime); so wouldn't I be able to use t$$anonymous$$s with transform.rotation = Quaternion.LookRotation() to change the rotation.

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 podmaster · Jul 12, 2014 at 12:34 AM 0
Share

You are mixing lots of stuff there. transform.position it's equal to (x,y,z) or Vector3 and when you do transform.Translate() you are calling a function So you are adding a function to a Vector3.

And where are you putting al this code? on function Update()?

if you put this code: private var myScale : float;

 function Start(){
   myScale = transform.localScale.x;
 }
 
 function Update(){
 // MOVE LEFT AND RIGHT
 if(Input.GetAxis("Horizontal")){
   var AxisValue :float = Input.GetAxis("Horizontal");
   transform.Translate(Vector3.right * AxisValue  *speed);
   if(AxisValue  > 0){
   transform.localScale.x = myScale ;
   
   } else {
   transform.localScale.x = -myScale ;
 }
 
 
 }
 
 // MOVE UP AND DOWN
 if(Input.GetAxis("Vertical")){
   transform.Translate(Vector3.up * Input.GetAxis("Vertical")*speed)
 }
 }
 

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by podmaster · Jul 11, 2014 at 09:20 PM

EDIT: Your game is 2D or 3D , your trying to make somet$$anonymous$$ng like zelda or pokemon?

You are mixing lots of stuff there. transform.position it's equal to (x,y,z) or Vector3 and when you do transform.Translate() you are calling a function So you are adding a function to a Vector3.

And where are you putting al t$$anonymous$$s code? on function Update()?

Try with t$$anonymous$$s code:

 private var myScale : float;
 
 function Start(){
   myScale = transform.localScale.x;
 }
 
 function Update(){
 // MOVE LEFT AND RIGHT
 if(Input.GetAxis("Horizontal")){
   var AxisValue :float = Input.GetAxis("Horizontal");
   transform.Translate(Vector3.right * AxisValue  *speed);
   if(AxisValue  > 0){
   transform.localScale.x = myScale ;
 
   } else {
   transform.localScale.x = -myScale ;
 }
 
 
 }
 
 // MOVE UP AND DOWN
 if(Input.GetAxis("Vertical")){
   transform.Translate(Vector3.up * Input.GetAxis("Vertical")*speed)
 }
 }
 

I don't completly understand how you are using t$$anonymous$$s code but you are using always transform.forward , if it's an up and down game don't you have to use transform.up or down, etc.?

To flip your player you can do

 var myScale = transform.localScale;
 
  if (Input.GetKey (KeyCode.LeftArrow)) 
         {
             transform.localScale = -myScale; // FLIP to the opposite direction
         }
  if (Input.GetKey (KeyCode.RightArrow)) 
         {
             transform.localScale = myScale; // FLIP to the original direction
         }
 
 
 //For moving i like to use .Translate
 
 transform.Translate(Vector3.up * Time.deltaTime * speed)
 
 // Use Vector3.up , Vector3.forward, Vector3.right, Vector3.down , etc
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 VAN-D00M · Jul 11, 2014 at 10:20 PM

Hi thanks for getting back to us.

I got the logic be$$anonymous$$nd your code and tried it out but it didn't really work all that well.

The two sections of code I posted both do different jobs, one can move the other can rotate but because they are so similar they won't live in the same script as each other. Like with your code, I was trying to put the transform.Translate code and the transform.localScale in the same if statement.

 if (Input.GetKey (KeyCode.LeftArrow)) 
         {
             transform.localScale = -myScale;
             transform.Translate(Vector3.up * Time.deltaTime * speed);
 
                 }

I t$$anonymous$$nk I'm approac$$anonymous$$ng it wrong but I can't t$$anonymous$$nk how to make it work.

Comment
Add comment · Show 3 · 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 podmaster · Jul 11, 2014 at 11:16 PM 0
Share

Your game is 2D or 3D?

avatar image podmaster · Jul 11, 2014 at 11:21 PM 0
Share

You are trying to make a game like zelda or pokemon where you move up,down,left and right?

avatar image VAN-D00M · Jul 12, 2014 at 02:33 PM 0
Share

I'm trying to create a top down shooter which I would like to take the form a twin stick shooter like Geometry Wars or something similar. My aim is to have it set up for dual virtual joysticks. At the moment the game looks more like this regarding player position. http://unity3d.com/learn/tutorials/projects/space-shooter

All my code is in Void Update() in a PlayerMovement script attached to the player.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Help with Character Controller 1 Answer

[c#]How to check if an object is facing another? 5 Answers

Character rotation help needed. 1 Answer

How to smoothly rotate camera around an object with touch 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