Hey guys,
This is for iPhone:
I’m wondering how to make a character, or object, move in a particular direction based on a button press – without having to hold the button down.
I’ve been searching the forum but I’m not sure the exact terms to search for.
Thanks a lot for your help!
There are a number of things you can do. One super simple method is to set a variable when the player presses a button, and then check for that variable in the Update() function. For example, if (moveNorth == true) { transform.position += new Vector3(0,0,1); } and then set moveNorth to true when the player presses the button.
@Dreamwriter – Thanks for your quick response!
I was able to find some code that is very similar to what you mentioned and very close to what I need. (I think)
I’ll need to study it a while to figure out what it means and how to piece it all together. (Still very new to UnityJS).
I think it’s a good sample to begin with because I will need collision detection to trigger animation, sound, etc.
I’ll see how far I can get with it but I’ll probably be back.
Stay tuned. And thanks!
Sample:
Change Direction on Collision
var isMoved = true;
var change = false;
function Update () {
if (isMoved == true)
transform.Translate(Vector3(1,0,0) * Time.deltaTime);
if (change == true)
transform.Translate(Vector3(-1,0,0) * Time.deltaTime);
}
function OnCollisionEnter (hit:Collision){
if (hit.gameObject.tag == "wallRight")
{
isMoved = false;
change = true;
Debug.Log("hit");
}
if (hit.gameObject.tag == "wallLeft")
{
change = false;
isMoved = true;
Debug.Log("hit");
}
}