Something wrong with my movement script...

Just a quick point-out: This script SHOULD have bools to move, I did not do it out of lack of programming experience. I did this so I can access the movement from other scripts whilst still having it move… However it seems like it is due to my lack of programming experience, otherwise I wouldn’t be here seeking the wisdom of your beautiful gray-matter.

Something is wrong, it is not moving. Can someone assist me? Or perhaps suggest a better approach? Thanks in advance :smiley:

Here is my script:

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {

	public static bool IsAlive = true;

	public static bool isMovingRight = false;
	public static bool isMovingLeft = false;

	public float speedVar;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {

		if(Input.GetKey(KeyCode.A)) {
			isMovingLeft = true;
		}

		if(Input.GetKey(KeyCode.D)) {
			isMovingRight = true;
		}

		//SWAGDIVIDERALLHAILLORDSWAGAXXUSPLZNOMALFURIONSPAMMANYLEGENDARYDOGECATELOGE

		if(isMovingLeft = true) {
			transform.Translate(Vector3.left * Time.deltaTime * speedVar);
		}

		if(isMovingRight = true) {
			transform.Translate(Vector3.right * Time.deltaTime * speedVar);
		}

		if (transform.position.x > 5) {
			transform.position = new Vector2(5, transform.position.y); // can't go further than 5
		}

		else if (transform.position.x < -5){
			transform.position = new Vector2(-5, transform.position.y); // can't go further than -5
		}

	}

	void OnCollisionEnter(Collision other) {
		if(other.gameObject.CompareTag("Kill")) {
			Death();
			Destroy(this.gameObject);
		}
	}

	void Death() {
		IsAlive = false;
		PlayerPrefs.SetInt ("Score", Score.scoreValue);
	}
}

Thanks so much you magnificent community :smiley:

Something that immediately sticks out is on line 31 and 35, you have:

if(isMovingLeft = true)

AND

if(isMovingRight = true)

Both of these statements will always evaluate to true because you are using assignment (single equals sign), so instead use the following:

if(isMovingLeft)

AND

if(isMovingRight)

Note: you could also use == true to evaluate a comparison.

Hope this helps!

You used a =, not == to check for the condition silly.