Answers for "unity 2d movement"

C#
5

how to make 2d movement in unity

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

public class movement2D : 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 May-17-2021
16

movement script c#

using UnityEngine;
using System.Collections;

// This script moves the character controller forward
// and sideways based on the arrow keys.
// It also jumps when pressing space.
// Make sure to attach a character controller to the same game object.
// It is recommended that you make only one call to Move or SimpleMove per frame.

public class ExampleClass : MonoBehaviour
{
    CharacterController characterController;

    public float speed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;

    private Vector3 moveDirection = Vector3.zero;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        if (characterController.isGrounded)
        {
            // We are grounded, so recalculate
            // move direction directly from axes

            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
            moveDirection *= speed;

            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }

        // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
        // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
        // as an acceleration (ms^-2)
        moveDirection.y -= gravity * Time.deltaTime;

        // Move the controller
        characterController.Move(moveDirection * Time.deltaTime);
    }
}
Posted by: Guest on June-05-2020
12

movement script unity

//make sure to add a CharacterController to the thing that you want to move
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    CharacterController characterController;

    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public float speed = 9.0f;

    private Vector3 moveDirection = Vector3.zero;

    private void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        var horizontal = Input.GetAxis("Horizontal");
        var vertical = Input.GetAxis("Vertical");

        transform.Translate(new Vector3(horizontal, 0, vertical) * (speed * Time.deltaTime));

        if (characterController.isGrounded)
        {

            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
            moveDirection *= speed;

            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }
        moveDirection.y -= gravity * Time.deltaTime;
        characterController.Move(moveDirection * Time.deltaTime);
    }
}
Posted by: Guest on October-26-2020
6

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
1

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
-2

unity 2d movement

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

public class PlayerMovement : MonoBehaviour
{
	bool isGrounded = true;
    RigidBody2D rb;
    
    
    private void Start() 
    {
    	rb = GetComponent<RigidBody2D>();
    }
    
    private void FixedUpdate()
    {
    	if(isGrounded && Input.GetButtonDown("Jump")) 
        {
        	rb.AddForce(new Vector2(0, 200));
			IsGrounded = false;
        }
        
        isGrounded = true;
    }	
}
Posted by: Guest on August-21-2021

C# Answers by Framework

Browse Popular Code Answers by Language