Hey guys I’ve been searching for a while and couldn’t find a solution specifically for my needs. I’m working on a school project where my only current need is to make an object move horizontally through the x-axis, which is easy enough, but I cannot figure out how to make it work through the default values set in Unity, such as using the arrow keys and then setting A and D for the alternate negative-X and positive-X movements.
Here is my current code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed = 5f;
private Rigidbody2D rb2d;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKey(KeyCode.A))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode. D))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
}
}
I tried using a code snippet I found from a tutorial on the Unity website but it was for using force and I couldn’t modify it for my needs. Unfortunately, I cannot use physics on my player object because it’s a kinematic rigidbody2d.
Could anyone provide insight on this?