I can't move left or right with my moving script
Hello guys ! First post here. I'm not a programer at all but for my video game art diploma i need to make some scripts in order to get a playable game. I'm working on a third person game and I had a question about this character controller script I copied from this Youtube Tutorial : Melee combat with Unity 5
My problem is, i can perfectly move on the Z axis with a decent speed, the script lauches the run animation of my character and i can move left or right by moving the camera (cinemachine brain targetting my character). But the problem appears if i want to move right or left (so when i want to use the X axis i presume) by pressing Right and Left Arrows. My character is moving, staying idle but moving but this is so slow ! I can increase the float speed in my public function and get a decent side step speed, but the Z axis is increased too and my character is too fast if i press Up or Down arrow. I would like to get the same speed for Forward/Backward AND side movements. Idid a quick video to show you the problem : Character Controller Script Issue
I think there is a solution somewhere in the script to fix that, but i'm too noob at C# to find where it is. I'll be really happy if you, skill full programers could take look at it and see where my problem is. Thank you in advance !
Here is the script (i added some lines to it ) :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class characterController1 : MonoBehaviour {
static Animator anim;
public float speed = 10.0F;
public Slider healthbar;
public ParticleSystem particles;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
//Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update ()
{
if (healthbar.value <= 0) return;
//
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 100.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
var y = Input.GetAxis("Horizontal") * Time.deltaTime * 3.0f;
//
transform.Rotate(0, x, 0);
transform.Translate(0, 0, z);
float translation = Input.GetAxis ("Vertical") * speed;
float straffe = Input.GetAxis ("Horizontal") * speed;
straffe *= Time.deltaTime;
transform.Translate (straffe, 0, translation);
//if (Input.GetButtonDown ("right"))
//transform.Translate(2, 0, z);
if (Input.GetButtonDown ("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
particles.Play();
anim.SetBool ("isAttacking", true);
}
else
anim.SetBool ("isAttacking", false);
if (translation != 0)
{
anim.SetBool ("isWalking", true);
anim.SetBool ("isIdle", false);
}
else
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isIdle", true);
}
if (Input.GetKeyDown ("escape"))
Cursor.lockState = CursorLockMode.None;
}
}
Answer by Hellium · Mar 30, 2018 at 12:16 PM
float straffe = Input.GetAxis ("Horizontal") * speed;
straffe *= Time.deltaTime;
Remove the 2nd line
Your answer
![](https://koobas.hobune.stream/wayback/20220612155657im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
fps script not working 1 Answer
WASD Controls Inverted? 0 Answers
Help on how to code crouching? 0 Answers
What is the problem with the forward motion in this script? 0 Answers
How to handle a Microsoft Controller? C# 0 Answers