Answers for "2d player movement unity script"

C#
0

2d movement unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
	//for A&D  /  Right&Left Arrow And Jump Movement
    //for Adding W&S Movement, Remove The Slashes Below And Add Slashes 
    //To Jump Movement Only Also In RigidBody2D Remove Gravity For It
    
    public Rigidbody2D rb;
    private bool IsGrounded = true;
    private float MoveX;
    //private float MoveY;
    private bool DoDeath = true;
    public GameObject DeathScreen;
    public GameObject LevelCompleteScreen;
    void Start()
    {
        DeathScreen.gameObject.SetActive(false);
        LevelCompleteScreen.gameObject.SetActive(false);
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Debug.Log(IsGrounded);
        if(Input.GetButtonDown("Jump") && IsGrounded)
        {
        	//Remove These 2 Lines Below For W&s Movement Only
            
            rb.AddForce(new Vector2(0,500));
            IsGrounded = false;
        }

        MoveX = Input.GetAxisRaw("Horizontal");
        Remove The Slashes For W&S Movement Only
        //MoveY = Input.GetAxisRaw("Vertical");
        //Slash The Below Line Out For W&S Movement Only
        
        rb.AddForce(new Vector2(MoveX * 20, 0));
        //Remove The Slashes For W&S Movement Only
        
        //rb.velocity = (new Vector2(MoveX * 20, MoveY * 20));
        
        
    }
    public void OnTriggerEnter2D(Collider2D ob)
    {
        if(DoDeath)
        {
            if(ob.name == "DeathTrigger")
            {
                DeathScreen.gameObject.SetActive(true);
            }
            if(ob.name == "Finish")
            {
                LevelCompleteScreen.gameObject.SetActive(true);
                DoDeath = false;
            }
        }
    }
    public void OnCollisionEnter2D()
    {
        IsGrounded = true;
    }
}
Posted by: Guest on July-23-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