【问题标题】:Is there a easy way to make a movement script for 2d unity game?有没有一种简单的方法可以为 2d unity 游戏制作移动脚本?
【发布时间】:2020-01-16 15:16:34
【问题描述】:

我需要 Unity 中的 C# 方面的帮助。我非常了解 C#,但不知道 C# UnityEngine 命名空间。

Unity 不支持足够的移动脚本,因为我不知道如何使用 C# for Unity,我不想尝试编辑主代码。

我对使用此命名空间的任何代码感到困惑:

using UnityEngine;

我对这样的变量感到困惑:

public float speed = 6.0F;
public float jumpSpeed = 8.0F; 
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;

【问题讨论】:

    标签: c# unity3d-2dtools


    【解决方案1】:

    这是一个简单的 2d 角色移动控制器。为了使该脚本正常工作,您必须:

    1。将此脚本分配给您的角色。

    2。为你的角色添加一个 RigidBody2d。

    3。从你的角色的检查器中为 speed 和 jumpHeight 变量赋值。

    public float speed;    
    public float jumpHeight; 
    Rigidbody2D rb; 
    
    void Start(){
        //Get the rigidbody2d of the gameObject this script is assigned to.
        rb = GetComponent<Rigidbody2D>();
    }
    
    void Update() {
        //Determine the direction of the movement based on user input.
        float moveDir = Input.GetAxis("Horizontal");
    
        //Calculate the velocity of the gameObject.
        rb.velocity = new Vector2(moveDir * speed, rb.velocity.y);
    
        // Your jump code:
        if (Input.GetKeyDown(KeyCode.Space))
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
        }
    }
    

    希望这对你有用。如需更多帮助,请查看unity官方turtorials

    【讨论】:

      猜你喜欢
      • 2017-08-12
      • 2020-02-12
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 1970-01-01
      相关资源
      最近更新 更多