0
\$\begingroup\$

I have a game idea... and I need a script where I can control the speed for moving left, and control another speed for moving right. But, I also need to let my player face that direction.

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            Vector3 position = this.transform.position;
            position.x -= .5f;
            this.transform.position = position;
        }
        
        if (Input.GetKey(KeyCode.RightArrow))
        {
            Vector3 position = this.transform.position;
            position.x += 1f;
            this.transform.position = position;
        }
        
        if (Input.GetKey(KeyCode.A))
        {
            Vector3 position = this.transform.position;
            position.x -= .5f;
            this.transform.position = position;
        }

           
        if (Input.GetKey(KeyCode.D))
        {
            Vector3 position = this.transform.position;
            position.x += 1f;
            this.transform.position = position;
        }
    }
}

That is my code. I need make it better I guess... and to make the player face that direction.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Define better. Do you want to have your code refactored? Do you want to have better conditions? Do you want to use the new input system? Does your game uses physics or is expected to collide with things? Did you check the documentation regarding flip property of sprites for your facing direction problem? \$\endgroup\$
    – Zibelas
    Feb 11 at 21:05
  • \$\begingroup\$ look i am just a beginner i want a code that is good for 2d platform that have the condition that i mentioned. thank you and yes my game have physics \$\endgroup\$ Feb 11 at 21:08
  • \$\begingroup\$ You might be better to start with a youtube tutorial about 2D platformers, try to reimplement it and ask a specific question if you encounter an error. As a starter, moving via transform.position will ignore physics/ collisions, you would want to move around with your rigidbody \$\endgroup\$
    – Zibelas
    Feb 11 at 21:12
  • \$\begingroup\$ just forget the code that i sent, can you give me advices and help me to write a good code, with physics 2D. if you dont want waste your time with me thats definitly not a problem \$\endgroup\$ Feb 11 at 21:18
  • \$\begingroup\$ This is not a code request stackexchange but aimed to help people with a specific problem they encounter while doing their game. You could check unity tutorials offered by unity itself for a starting 2D game that will walk you step by step. once you understand how everything works together, you can bring your own ideas into it. And if you have problems implementing them, you can ask here a question with the code you wrote, whats the error is and what you would expect it to do. learn.unity.com \$\endgroup\$
    – Zibelas
    Feb 11 at 21:24

1 Answer 1

0
\$\begingroup\$

If I understand the idea you want to achieve correctly, I would suggest that you :

  • Store Vector1 : a vector of movement speed values in different directions as you would like
  • Store Vector2 : another vector (Horizontal and Vertical Axis) for player inputs (more on that here).
  • Scale Vector1 by Vector2 which is component-wise multiplication store that in Vector3 (more on that here).

Now you have read the input from user and have V3 a vector representing the final velocity.

Add that to your player position by any update method.

You can also make it rotate and face the same vector(similar to this reference).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .