- Home /
How to make change variables
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class ballCtrl : MonoBehaviour
{
private bool isRightPressed = false;
private bool isLeftPressed = false;
public bool isCrashed = false;
private Rigidbody2D rb;
public float sensivity;
void Start () {
rb = GetComponent<Rigidbody2D> ();
}
void Update () {
if (!isRightPressed && !isLeftPressed) {
//Debug.Log ("What the...?");
return;
} else if (isLeftPressed) {
//Left
rb.AddForce (Vector2.left * Time.deltaTime * sensivity);
Debug.Log ("Left Go");
} else {
//Right
rb.AddForce (Vector2.right * Time.deltaTime * sensivity);
Debug.Log ("Right Go");
}
}
public void LeftDown(){
isLeftPressed = true;
Debug.Log ("Left Down");
}
public void LeftUp(){
isLeftPressed = false;
Debug.Log ("Left Up");
}
public void RightDown(){
isRightPressed = true;
Debug.Log ("Right Down");
}
public void RightUp(){
isRightPressed = false;
Debug.Log ("Right Up");
}
}
How to make this move?? Actor that this script connected aren't moving.
public float sensivity;
is not 0, and also I checked that these functions are called. (LeftDown, LeftUp, RightDown, RightUp)
PLZ HELP
Answer by CalleRC · Jul 29, 2017 at 11:34 AM
@SkytreeNetwork Well I'm not quite sure, but first of all, you will need to define the boolean's "isRightPressed" and "isLeftPressed", and then you will need to change their values from false to true upon a button press. This can be done with the following code.
private bool isRightPressed = false, isLeftPressed = false;
void Update() {
if (Input.GetKey(KeyCode.RightArrow))
isRightPressed = true
else
isRightPressed = false
if(Input.GetKey(KeyCode.LeftArrow))
isLeftPressed = true
else
isLeftpressed = false
}
but instead of making variables depending on the key is pressed, you can just apply the force to the rigidbody inside the "if" statement.
Not sure what you are trying to make, but if its a fps controller, you can use the FPS asset that is premade in unity, or follow this tutorial. It is well explained and though me a lot about making character movements.