- Home /
Changing a variable from a different method
Hello, I'm new to both unity, and the world of programming. I've been working on my first game in unity. I'm trying to convert a premade controller I found to have touch screen abilities. After watching a few tutorials on youtube I found the easiest way to do this is by using a second script (Touch controller) The Player controller I'm using however is far more complex then those used in the tuts so I'm very lost on what I'm doing.
public void ProcessInput()
{
float inputAxisX = Input.GetAxis("Horizontal");
if (inputAxisX == 0)
{
Debug.Log("AxisX = 0");
}
else
{
Debug.Log("Moving! " + inputAxisX);
}
bool isKeyDownJump = Input.GetButtonDown
("Jump");
bool isKeyDownLeft = inputAxisX <-.5;
bool isKeyDownRight = inputAxisX > .5;
This seems to be the most important code when it comes to input. I need a way to change the inputAxisx from a different method that I named move for the player to move when my on screen buttons are touched with unitys built in event trigger system with pressdown, and pressup
public void Move()
{
}
But I'm not sure how to go about this.
Answer by TheSOULDev · Aug 20, 2017 at 03:08 AM
So this is your code:
public void ProcessInput()
{
float inputAxisX = Input.GetAxis("Horizontal");
if (inputAxisX == 0)
Debug.Log("AxisX = 0");
else
Debug.Log("Moving! " + inputAxisX);
bool isKeyDownJump = Input.GetButtonDown ("Jump");
bool isKeyDownLeft = inputAxisX < -0.5f;
bool isKeyDownRight = inputAxisX > 0.5f;
}
I'm assuming you want to make your Move move the character when isKeyDownLeft or isKeyDownRight is on, right?
Now I'm not sure what you mean by changing variables from a different method. Methods are like functions; if you want to give a function variables, you just send it arguments. From what you want it seems like you need code to go from one script to another.
That is done in several ways - one is you could make the variables you need static, essentially telling Unity that those variables have one instance, and then you don't need to specify the gameobject holding the script to change them because Unity just changes all of them, OR you will have copies of your script which will then be on a gameobject, which you can reference later. I'd assume as a starter you want the latter. First of all, the method you have needs to either return something or have the bools it changes outside of the method. Let's say you have another script called "Player". This script would look something like this:
public class Player : MonoBehaviour
{
public TouchController TouchCont;
Rigidbody2D charRB;
const float speed = 1.0f;
void Start ()
{
TouchCont = FindObjectOfType<TouchCont>();
charRB = GetComponent<Rigidbody2D>();
}
void Update()
{
if(TouchCont.isKeyDownLeft)
charRB.velocity = Vector2.left * speed;
else if(TouchCont.isKeyDownRight)
charRB.velocity = Vector2.right * speed;
else
charRB.velocity = Vector2.zero;
}
}
While a part of your original script would look something like this:
public class TouchController : MonoBehaviour
{
public bool isKeyDownJump, isKeyDownLeft, isKeyDownRight;
public void ProcessInput()
{
float inputAxisX = Input.GetAxis("Horizontal");
if (inputAxisX == 0)
Debug.Log("AxisX = 0");
else
Debug.Log("Moving! " + inputAxisX);
isKeyDownJump = Input.GetButtonDown ("Jump");
isKeyDownLeft = inputAxisX < -0.5f;
isKeyDownRight = inputAxisX > 0.5f;
}
void Update()
{
ProcessInput();
}
}
However, this is a really crappy implementation and not optimized. Before going further into creating your game, I'd advise you to go through Beginner, Intermediate and Editor scripting tutorials here https://unity3d.com/learn/tutorials/s/scripting (top 3), watch them as many times as you need and hopefully you'll be a better programmer. I didn't add the jump movement but I think you'll be able to fill in the blanks after watching the tutorials.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I flip only my 'Player' gameobject, and l leave it's child object alone? 2 Answers
Flipping a 2D Game object over time 1 Answer
HOLD JUMP BUTTON TO JUMP HIGHER 2 Answers