• 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 KrookedDoesStuff · 3 days ago · beginnermovement script

Issues with player movement

Hey all,

I've looked at a lot of solutions, I've tried out a bunch, and I'm just not finding an answer, and I've tried re-writing my code a few times and it just hasn't worked out. What I'm currently working with is below, and I've been able to make my character shoot a projectile, I've been able to make them jump pretty well, but movement is being a pain.

What I'm attempting to do is make a 2D sprite move left and right with keyboard input (A and D respectively) and when it moves left and right, I want it to rotate 180 on the y so it faces the direction that it's trying to move. Right now when I hit "D" it turns the sprite to the right, and starts moving left. When I push "A" it moves to the left and doesn't rotate the sprite.

I know this is probably a really simple fix, but I'm very new to Unity, and I can't seem to figure this out. I did have a basic movement script before using transform.translate and that allowed me to move left and right but it seemed like the scripting I have now is more realistic?

Also if there's any way I can clean this up and make it better, I'd love to hear. Thank you all in advance!

using UnityEngine;

public class PlayerControl : MonoBehaviour { private Rigidbody2D playerRb;

 public GameObject shotPrefab;
 public Vector2 speed = new Vector2(10, 50);
 public Vector3 turnRotation = new Vector3(0, 180, 0);

 public float horizontalInput;
 public float verticalInput;
 public float jumpForce;
 public float gravityModifier;



 public bool isOnGround;

 // Start is called before the first frame update
 void Start()
 {
     playerRb.GetComponent<Rigidbody2D>();

 }

 // Update is called once per frame
 void Update()
 {
     horizontalInput = Input.GetAxis("Horizontal");

     // Move the player using interface

     Vector3 movement = new Vector3(speed.x * horizontalInput, 0, 0);


     movement *= Time.deltaTime;

     transform.Translate(movement);

     if (horizontalInput > 0) // Rotates character 90 degrees based on input
     {
         transform.localEulerAngles = turnRotation;
     }
     if (horizontalInput < 0)
     {
         transform.localEulerAngles = -turnRotation;
     }

     // Allows the player to jump
     if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
     {
         GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
         isOnGround = false;
     }

     if (Input.GetKeyDown(KeyCode.LeftShift))
     {
         //Launch a projectile from the player
         Instantiate(shotPrefab, transform.position, shotPrefab.transform.rotation);

     }


 }
 public void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.tag == "Ground")             // Verifies player is on ground

     {
         isOnGround = true;
     }
 }

}

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
0

Answer by TheTrueDuck · 15 hours ago

Hi! I think the best way to achieve something like this would be to have basic movement on an invisible gameobject- no rotation or flipping of any kind, just moving left, right and jumping - and giving this gameobject a child which will actually show any player sprites and their orientation.

This separation of concerns is great because the player movement scripts will never have to worry about what orientation the player is in, instead you can have a separate script on the art, that just checks which key is being pressed and flips or mirrors accordingly.

Similarly you can have a script that shoots projectiles, where all it does it remember direction faced and waits for a button press.

If you are up for the challenge, there are actually many ways of making your player feel even more fun to control. Check out this talk if you're interested: youtube.com/watch?v=hG9SzQxaCm8

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

161 People are following this question.

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

Related Questions

My player still keep moving when i unparent it form the car 2 Answers

I need help with my script for my squirrel 1 Answer

How do I stop moving when attacking? 1 Answer

How to move the other object without moving the first object? 1 Answer

How to move the object to where the object is already pointing to? 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