matturp
December 13, 2014, 4:26pm
1
Hi, haven’t been using unity for long and my knowledge of C# is quite lacking. I have a 2D sprite for a platformed which I’ve assigned a run animation to. I’m having trouble assigning code so that the animation will play both when he’s running right and left (currently it’s just left). Here’s the code:
voidMoveCharacter (){
if (Input.GetKey (KeyCode.LeftArrow)) {
run = true;
} else {
run = false;
}
if (Input.GetKey (KeyCode.Space)) {
jump = true;
} else {
jump = false;
}
animator.SetBool (“run”, run);
animator.SetBool (“jump”, jump);
}
Any help with this? Thanks!
Hi,
I’m new too, but I think have a solution for you.
Why not just simply rotate the x axis to the negate.
Like that:
void Flip(){
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
ahem you have only if (Input.GetKey (KeyCode.LeftArrow)) { there
You need to take account also RightArrow.
something like that ?
voidMoveCharacter (){
if (Input.GetKey (KeyCode.LeftArrow)) {
run = true;
jump = false;
} else {
run = false;
}
if (Input.GetKey (KeyCode.Space)) {
jump = true;
run = false;
} else {
jump = false;
}
animator.SetBool (“run”, run);
animator.SetBool (“jump”, jump);
}
Sir (or madam), I’m going to make you a big favor: what if you’d take it slowly and carefully read what was written… and then try it again.
Good luck.
matturp
December 15, 2014, 7:42am
6
No I get that, I know how to code it for left OR right, I just can’t figure out how to do both…
You can achieve what you want to do by using an operator after the Input for the left. Have a look at these pages:
If you need more help, don’t be hesitated.
@sandbaydev : I don’t want give all the code… I just want explain.
You can detect ! See that:
using UnityEngine;
using System.Collections;
public class playerControlScript : MonoBehaviour {
public float maxSpeed = 50;
private Vector2 movement;
private bool isRight;
void Update () {
movement = new Vector2(
inputX * maxSpeed,
0);
if (inputX > 0 && isRight == true) {
Flip();
isRight = false;
}
if (inputX < 0 && isRight == false) {
Flip();
isRight = true;
}
}
void FixedUpdate()
{
rigidbody2D.velocity = movement;
}
void Flip(){
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}