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 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 which 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 which makes me think there must be another way and after looking at everyoones else's questions and solutions, I am stumped.

This 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 this 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 which 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 think 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, this is the back up which hasn't been changed to match. :)

EDIT:

Why won't this work?

 private Vector3 direction; //something like this??
 
 void Update()
 {
       if (Input.GetKey (KeyCode.UpArrow)) 
         {
             direction = transform.Translate(Vector3.forward * speed * Time.deltaTime);
             transform.rotation = Quaternion.LookRotation(direction);
                 }
 }

I know this moves the player transform.Translate(Vector3.forward speed Time.deltaTime); so wouldn't I be able to use this 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(){
 // $$anonymous$$OVE 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 ;
 }
 
 
 }
 
 // $$anonymous$$OVE 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 something 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 this code? on function Update()?

Try with 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)
 }
 }
 

I don't completly understand how you are using this 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 behind 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 think I'm approaching it wrong but I can't think 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. $$anonymous$$y 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 Player$$anonymous$$ovement 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.

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

Player Controller Rotation Script Help 2 Answers

How to make camera move backwards in relation to player (so behind player), regardless of y rotation of player and camera 1 Answer

Help with Character Controller 1 Answer

[c#]How to check if an object is facing another? 5 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