Help creating character controller(2d) without a rigidbody2d

I have asked parts of this question before, but I always run into something else that is preventing me from completing this script. I want to make a 2d game, but I don’t want to use this physics engine, I just want the character to move up and down the y or x axis when i use the arrow keys. basically, when input.getaxis(“vertical or horizontal”) i want a variable that controlls the x and y transform position to increment per frame. I have tried increment transform.position.x(or y), but I can’t edit that directly, so I don’t know what to do. Ideas?

using UnityEngine;
using System.Collections;

public class Mover2D : MonoBehaviour {

    //Set in the editor.
    [SerializeField] float speed = 1f;

    void Update ()
    {
        Move ();
        Turn ();
    }

    void Move()
    {
        float h = Input.GetAxis ("Horizontal");
        float v = Input.GetAxis ("Vertical");
       
        //We want to apply a vertical movement from this transform's upward vector.
        //This way, if the character is rotated to the right, pressing up will make
        //it walk to the right; forward from it's perspective
        var move = transform.up * v;
       
        //Similar idea applies to horizontal movement. We want it to sidestep right or
        //left depending on it's orientation.
        move += transform.right * h;
       
        //We don't want to give the character a speed bonus for pressing both
        //keys at once. How this works is a bit complex to explain quickly,
        //so you'll just have to trust me.
        if (move.sqrMagnitude > 1f)
            move = move.normalized;
       
        //And we position the transform accordingly.
        transform.position += move * speed * Time.deltaTime;
    }

    void Turn()
    {
        //Perspective screen point
        Vector3 perScreen = Input.mousePosition + Vector3.forward * -Camera.main.transform.position.z;

        //2D Mouse position relative to this transform
        Vector2 mouseRel = Camera.main.ScreenToWorldPoint (perScreen) - transform.position;
       
        //We get the angle of the mouse position with this formula
        float angle = Mathf.Atan2 (mouseRel.y, mouseRel.x) * Mathf.Rad2Deg - 90f;
       
        //Then we rotate the transform accordingly.
        transform.localEulerAngles = Vector3.forward * angle;
    }
}
1 Like

I made a quick cheese one that is useful for 2d platformers, jumping, flight, etc.

I’ll toss it up here anyway. Left/Right to accelerate, Space to jump… if you want to have real collisions beyond the hard-wired ones, you’d need to code it up.

I put 3D objects on it just so I wouldn’t have to make sprites.

2408335–164526–Ballistic2D.unitypackage (4.37 KB)

That was really helpful, but I don’t want the character to turn based on the mouse and the turn function confused me a bit. I want left and right arrows to control the x axis, and up and down to control the y axis

then just remove the Turn() function!