Reputation: 1
I'm relatively new to coding, and I've run into a small problem with my code. I'm using Vector3 in unity to make my character move, but when I move diagonally, the character moves faster than vertically/horizontally. I used a boolean fMove to account for w and s presses, and then I used another one rMove for a and d presses. I tried to make it change the speed from 10 to 7.07 when both were true, and then return it from 7.07 to 10 when either or both is false.
My issue is that it only work once every 10 or so tries, and it only works for 1 second. I'm trying to adjust the priorities to get it to check the speed changing checks first, but I'm unsure if that's the correct idea. Any help would be much appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Movement : MonoBehaviour
{
//Defining Variables
public float speed = 10.0f;
public float jumpHeight = 4.0f;
private float rightInput;
private float forwardInput;
private float jumpInput;
private bool rMove;
private bool fMove;
void Start()
{
rMove = false;
fMove = false;
}
void Update()
{
//Retrieve Player Input
rightInput = Input.GetAxis("Horizontal");
forwardInput = Input.GetAxis("Vertical");
jumpInput = Input.GetAxis("Jump");
//Player Movement
transform.Translate(Vector3.forward * speed * Time.deltaTime * forwardInput);
transform.Translate(Vector3.right * speed * Time.deltaTime * rightInput);
transform.Translate(Vector3.up * jumpHeight * Time.deltaTime * jumpInput);
//Set booleans for movement speed correction
if(Input.GetKeyDown("w") || Input.GetKeyDown("s"))
{
fMove = true;
}
else if(Input.GetKeyDown("w") == false || Input.GetKeyDown("s") == false)
{
fMove = false;
}
if(Input.GetKeyDown("a") || Input.GetKeyDown("d"))
{
rMove = true;
}
else if(Input.GetKeyDown("a") == false || Input.GetKeyDown("d") == false)
{
rMove = false;
}
//Adjust speed for diagonal movement
if(fMove == true && rMove ==true)
{
speed = 7.07f;
speed = 2.0f;
Debug.Log("move = " + speed);
}
else if(fMove == false || rMove == false)
{
speed = 10.0f;
Debug.Log("move = " + speed);
}
}
}
Upvotes: 0
Views: 130
Reputation: 117
For your movement, instead of using keys, you should use Axis (and buttons). Unity has a whole Input Manager system that allows you to bind keyboard and controllers to the action (like Fire1, Jump, Horizontal, Vertical, etc.).
A good example for a basic movement would be this:
[SerializeField] private Rigidbody rb;
[SerializeField] private float speed;
private float hAxis = 0;
private float vAxis = 0;
void Update() // All your inputs fetchers should stay in the Update method
{
hAxis = Input.GetAxisRaw("Horizontal");
vAxis = Input.GetAxisRaw("Vertical");
}
void FixedUpdate() // While your physics should stay in FixedUpdate method
{
Vector3 direction = Vector3.Normalize(new Vector3(hAxis, 0, vAxis)); // Converts keyboard and joystick to an uniform vector (same magnitude)
Vector3 newVelocity = rb.transform.TransformDirection(direction) * speed * Time.fixedDeltaTime;
rb.velocity = new Vector3(newVelocity.x, rb.velocity.y, newVelocity.z); // We copy y velocity to avoid the gliding common issue
}
Hopefully this helps!
Upvotes: 1