I’m new to unity and am trying to learn scripting. I thought it would be easier to just see what makes one action tick rather than looking at the whole controller code for the 2D tutorial. So, I just want to know what the bare minimum script necessary is - just walking, no running, etc. Thanks!
this moves the character on the X as with the Arrow keys or A and D.
Here you can add a speed Variable and the time.deltatime Variable.
Also you can start an animation when you do this.
Yes the Input.GetAxis is for more smooth gameplay. Also GetAxisRaw can be 0,5 but only with an anolog stick or steering wheel.
Also you can slow down the player with a Speed variable. So you can control his speed even more. It is also very important to use Time.deltaTime. This will move the player per Seconds and not per Frame.
And you are right on the “this” part. “this” stands for the GameObject where the script is attached to.
Also it is possible to make a public GameObject and control another gameobject trough another script. Like:
public GameObject Character;
public float Speed = 5f;
public bool canControl;
void Update()
{
if(canControl == true)
{
float h = Input.GetAxis("Horizontal") * Speed;
Character.transform.Translate(h*Time.deltaTime,0,0);
}
}
using UnityEngine;
public class PlayerController2 : MonoBehaviour
{
public float Speed = 5f;
void Update()
{
float h = Input.GetAxisRaw("Horizontal");
GetComponent<Rigidbody2D>().velocity = new Vector2(h, 0) * Speed;
}
}