- Home /
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 :D
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 :D
Answer by VioKyma · Feb 21, 2014 at 10:52 PM
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!
There's still a problem...
When I move, he only moves left, not right...
Additionally, the GUI buttons I have set up to take care of movements don't work as well...
Here is my GUI script:
using UnityEngine;
using System.Collections;
public class $$anonymous$$oveButtons : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnGUI () {
if(GUI.Button(new Rect(Screen.width - Screen.width, Screen.height - Screen.height, 300, Screen.height), "LEFT")) {
$$anonymous$$ovement.is$$anonymous$$ovingLeft = true;
}
if(GUI.Button(new Rect(Screen.width - 300, Screen.height - Screen.height, 300, Screen.height), "RIGHT")) {
$$anonymous$$ovement.is$$anonymous$$ovingRight = true;
}
}
}
Hope that helps :D
and if I remove the "= true" as you told me, it returns an error :'(
With the GUI buttons, you need to reference an actual instance of $$anonymous$$ovement, not the script itself. You have two options here, you can either pass the object the $$anonymous$$ovement script is attached to, or you can create and use a Singleton pattern.
For the first option, your script would look something like:
using UnityEngine;
using System.Collections;
public class $$anonymous$$oveButtons : $$anonymous$$onoBehaviour {
public GameObject movementObject;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnGUI () {
$$anonymous$$ovement moveScript = movementObject.GetComponent<$$anonymous$$ovement>();
if(GUI.Button(new Rect(Screen.width - Screen.width, Screen.height - Screen.height, 300, Screen.height), "LEFT")) {
moveScript.is$$anonymous$$ovingLeft = true;
}
if(GUI.Button(new Rect(Screen.width - 300, Screen.height - Screen.height, 300, Screen.height), "RIGHT")) {
moveScript.is$$anonymous$$ovingRight = true;
}
}
}
With the second, it's a bit more complicated, but check out this link here for a basic implementation: http://wiki.unity3d.com/index.php/Singleton
I am now getting the following errors:
Assets/$$anonymous$$oveButtons.cs(18,36): error CS0176: Static member
$$anonymous$$ovement.is$$anonymous$$ovingLeft' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d-
Assets/$$anonymous$$oveButtons.cs(22,36): error CS0176: Static member$$anonymous$$ovement.is$$anonymous$$ovingRight' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d
I do not know the cause of this issue, please reply asap. Thank you so much for your help thus far :D
Answer by j_perker · Feb 21, 2014 at 11:07 PM
You used a =, not == to check for the condition silly.