0
\$\begingroup\$

When using Horizontal or Vertical input keys, the the movement is normal. But when trying to move diagonal, multiple keys are to be pressed, and the speed just stacks up.

Yes there are many answers to such questions. But I'm having a hard time figuring out a solution. I am actually using an asset, and the code is kinda hard to understand for me. The script is on my drive and the link is below: https://drive.google.com/open?id=1Lt4DZBw7Jv2LNyYR-03aNUpV2a29F7dm

Please provide a solution. I am not so good at coding. And I'm in real need to make that game because of many reasons as fast as I can so I will learn all coding later. (and in the script please search for Move, Movement or related words to go to specific movement code, thank you a lot)

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Without looking at your code I can guess, that it has something along the lines of "if the left key is pressed, then set velocity.x to speed, if the forward key is pressed, then set velocity.y to speed". This means that the velocity when both key is pressed is √2 * speed \$\endgroup\$
    – Bálint
    Jan 6, 2018 at 14:37
  • \$\begingroup\$ the script is just so much hard for me to understand. really, im unable to do it.its just so complex after looking at the code :'( \$\endgroup\$
    – Prakash
    Jan 6, 2018 at 14:52
  • \$\begingroup\$ oh and yes Balint you are right. Its that maybe. But Im actually willing to get a solution based on the script :( \$\endgroup\$
    – Prakash
    Jan 6, 2018 at 14:56
  • 3
    \$\begingroup\$ Include the relevant code in the question itself, so it can't be lost to link rot. \$\endgroup\$
    – DMGregory
    Jan 6, 2018 at 15:03
  • 1
    \$\begingroup\$ If the code does not fit, then you have not done the work of creating a minimal, complete, verifiable example, which we require for questions involving debugging code. The type of feature you're describing can be summarized in a dozen lines or less. If you're unwilling to take the time to isolate that information from that giant linked script, why would anyone else? If you don't know how, I recommend throwing out that script and starting with something much simpler. \$\endgroup\$
    – DMGregory
    Jan 6, 2018 at 19:10

2 Answers 2

0
\$\begingroup\$

Try setting velocity to your desired velocity if both left and up (or right and down, etc) are pressed at the same time. That should do it.

\$\endgroup\$
4
  • \$\begingroup\$ what if right down and left are pressed at the same time? \$\endgroup\$
    – Wietlol
    Jan 6, 2018 at 15:50
  • \$\begingroup\$ @Wietlol you may not want to register movement for that. Or you may want to pick the last two pressed. \$\endgroup\$
    – nerk
    Jan 6, 2018 at 15:51
  • \$\begingroup\$ depends on how you do the user input, still, the best approach by far is getting axes values (-1 to 1) for x movement and y movement and figuring out the direction (in radians), then multiply the speed by the sin and cos to get the actual movement, if both axis values are close to 0 (not equal, just close), you dont apply the movement \$\endgroup\$
    – Wietlol
    Jan 6, 2018 at 15:59
  • \$\begingroup\$ please see this part of the script /// Moves the character according to the input. /// <param name="horizontalMovement">-1 to 1 value specifying the amount of horizontal movement.</param> /// <param name="forwardMovement">-1 to 1 value specifying the amount of forward movement.</param> public void Move(float horizontalMovement, float forwardMovement, Quaternion lookRotation) { // Store the velocity as it will be used by many of the functions below. m_Velocity = m_Rigidbody.velocity; // Store the input parameters. m_InputVector.x = horizontalMovement; m_InputVector.z = forwardMovement; \$\endgroup\$
    – Prakash
    Jan 6, 2018 at 16:40
0
\$\begingroup\$

If Unity3D:

this._inputs = Vector3.zero;
this._inputs.x = Input.GetAxis("Horizontal");
this._inputs.z = Input.GetAxis("Vertical");
if(this._inputs.magnitude > 1)
  this._inputs = this._inputs.normalized;
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can also use Vector3.ClampMagnitude to achieve this effect. And note that you usually won't need all these leading "this." keywords. It's implicit if there's not a local variable/argument shadowing the same name. \$\endgroup\$
    – DMGregory
    Jan 23, 2019 at 20:23

You must log in to answer this question.

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