- Home /
Question by
baconman249 · May 20, 2015 at 10:55 AM ·
c#beginnerkeyboard input
2D Character Control Help
Hey guys, I started using Unity yesterday, and have been working mainly with script all day trying to get familiar with the transition from being an only C programmer to using C#. Today I was following This Code and got stuck on moving my character. I initially followed along and copied the code word for word. I know that there are some syntactical changes that came with Unity 5, but correcting those leaves me with a script that doesn't respond to my key input.
My question is how do I tweak my current code to simply move my character.
using UnityEngine;
using System.Collections;
public class MovingScript : MonoBehaviour {
public float maxSpeed = 10f;
bool facingRight = true;
Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate ()
{
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Thanks for the help. I'm amazed with how awesome the Unity community is turning out to be so far!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613200039im_/https://answers.unity.com/themes/thub/images/avi.jpg)