“Movimiento de la Unidad 2D” Código de respuesta

Script de movimiento 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);
    }
}
Zealous Zebra

Movimiento Unidad 2D

// This movement is used for platformer type games
bool jump = false;
float MovementSmoothness = 0.1f;
public float JumpForce;
public PlayerSpeed;
Rigidbody2D rb;

void Start(){
  rb = GetComponent<Rigidbody2D>();
}
void Update()
{
  	horizontal = Input.GetAxisRaw("Horizontal");
  	if(Input.GetKeyDown(KeyCode.W) jump = true;
}

private void FixedUpdate()
{
  	if(jump){
      rb.AddForce(new Vector2(0f, JumpForce));
      jump = false;
    }
  	if(Input.GetKey(KeyCode.LeftShift))// If shift is pressed, sprint
            targetvelocity =  new Vector2(horizontal * PlayerSpeed * 2 * Time.deltaTime, rb.velocity.y);
    else // else go normally
            targetvelocity =  new Vector2(horizonal * PlayerSpeed * Time.deltaTime, rb.velocity.y);
  	// this makes movement much smoother ans satisfying
  	rb.velocity = Vector3.SmoothDamp(rb.velocity,targetvelocity, new Vector3(0,0,0), MovementSmoothness);
}
Xixika

Movimiento de la Unidad 2D

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

public class 2dMovement : MonoBehaviour
{
  //This also works with joystick, PS this is topdown movement

 private Rigidbody2D rb;
 public float MoveSpeed = 15f;


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

 void Update ()
 {
    private float vertical;
    private float horizontal; 
 
    horizontal = Input.GetAxisRaw("Horizontal");
    vertical = Input.GetAxisRaw("Vertical"); 
 }

 private void FixedUpdate()
 {  
    rb.velocity = new Vector2(horizontal * MoveSpeed, vertical * MoveSpeed);
 }
}
Levi Bills

Cómo hacer que el jugador se movimiento en Unity 2d

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

public class PlayerMovement : MonoBehaviour
{
	// this has jumping, movement, and the code for groundCheck. You must do
    // things yourself in the unity editor to for groundCheck. Code Tested
    // unity 2d platformer
    
    
    public float speed = 5f;
    public float jumpSpeed = 3f;
    private float direction = 0f;
    private Rigidbody2D player;

    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask groundLayer;
    private bool isTouchingGround;
    // Start is called before the first frame update
    void Start()
    {
        player = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        isTouchingGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
        direction = Input.GetAxis("Horizontal");

        if (direction > 0f)
        {
            player.velocity = new Vector2(direction * speed, player.velocity.y);
        }
        else if (direction < 0f)
        {
            player.velocity = new Vector2(direction * speed, player.velocity.y);
        }
        else
        {
            player.velocity = new Vector2(0, player.velocity.y);
        }

        if (Input.GetButtonDown("Jump") && isTouchingGround)
        {
            player.velocity = new Vector2(player.velocity.x, jumpSpeed);
        }

    }
}
Prickly Plover

Respuestas similares a “Movimiento de la Unidad 2D”

Preguntas similares a “Movimiento de la Unidad 2D”

Más respuestas relacionadas con “Movimiento de la Unidad 2D” en C#

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código