Answers for "unity player movement script c# 2d"

C#
5

unity 2d c# movement

//This is not a Plataformer type movement 
//Its more of like the old pokemon games type movement if you know what im talking about
//Body Type on RigidBody 2D must be set to kinematic
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
    Rigidbody2D body;

    float horizontal;
    float vertical;

    public float runSpeed = 10.0f;


    // Start is called before the first frame update
    void Start()
    {
        body = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
        vertical = Input.GetAxisRaw("Vertical");
    }

    private void FixedUpdate()
    {
        body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
    }
}
Posted by: Guest on June-17-2021
0

unity movement script 2d

//This is a script for a 2D plataformer type of movement.
//This script includes the movement, flipping the player and a ground check if you need one.
/*
  This is a two in one grepper answer, 
  the script flips the player by rotating it,
  and when it rotates, if the camera 
  is attached to the player the normal way, 
  its going to rotate the hole camera.
  and the only way to fix that bug is by making it so 
  the camera follows you with code, 
  if you scroll all the way down you can find the code that makes 
  the camera follow the player.
*/

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float movementSpeed = 1;
    private Rigidbody2D rb;
    public float JumpForce = 1;
    //If the character flipps the wrong way change this variable to false.
    public bool facingRight = true;
    public Camera cam;
    public bool isGrounded = false;
    public Transform GroundCheck;
    public LayerMask Ground;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        Xmovement();
        GroundCheck();
        Jump();
        
        void Xmovement()
        {

         var movement = Input.GetAxis("Horizontal");
         transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * movementSpeed;
                
            if (Input.GetAxis("Horizontal") > 0)
            {
                facingRight = false;
            }
            if (Input.GetAxis("Horizontal") < 0)
            {
                facingRight = true;
            }
            //Flipping the player
            if (facingRight) {
            transform.eulerAngles = new Vector3(0, 180, 0);
            }

        }

        void Jump()
        {
            if (Input.GetButtonDown("Jump") && Mathf.Abs(rb.velocity.y) < 0.001f)
            {
                rb.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
            }
        }

        void GroundCheck()
        {
            if (Mathf.Abs(rb.velocity.y) < 0.001f)
            {
                isGrounded = true;
            }

            else
            {
                isGrounded = false;
            }
        }
    }
}  





/*
	Read the commented lines on the top of the answer for context.
*/
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Camera cam;

    public Transform subject;

    // Update is called once per frame
    void LateUpdate()
    {
        cam.transform.position = subject.transform.position ;
    }
}
Posted by: Guest on September-28-2021

C# Answers by Framework

Browse Popular Code Answers by Language