Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 尝试创建一个好的角色控制器?_Unity3d - Fatal编程技术网

Unity3d 尝试创建一个好的角色控制器?

Unity3d 尝试创建一个好的角色控制器?,unity3d,Unity3d,我对统一的2D环境有点陌生 我正在尝试创建一个平台。现在,我有一个简单的地图和我的播放器 我的播放器附带了一个脚本: public class Player : MonoBehaviour { public float speed; public float jump; public GameObject raycastPoint; // Positioned at 0.01 pixel below the player private SpriteRende

我对统一的2D环境有点陌生

我正在尝试创建一个平台。现在,我有一个简单的地图和我的播放器

我的播放器附带了一个脚本:

public class Player : MonoBehaviour
{
    public float speed;
    public float jump;
    public GameObject raycastPoint; // Positioned at 0.01 pixel below the player

    private SpriteRenderer spriteRenderer;
    private Rigidbody2D body; // Gravity Scale of the Rigidbody2D = 50
    private Animator animator;

    private void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        body = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

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

        if (horizontal == 1 && spriteRenderer.flipX)
        {
            spriteRenderer.flipX = false;
        }
        else if (horizontal == -1 && !spriteRenderer.flipX)
        {
            spriteRenderer.flipX = true;
        }

        body.velocity = new Vector2(horizontal * speed, body.velocity.y);

        animator.SetFloat("Speed", Mathf.Abs(horizontal));

        float vertical = Input.GetAxisRaw("Vertical");

        if (vertical == 1)
        {
            RaycastHit2D hit = Physics2D.Raycast(raycastPoint.transform.position, Vector2.down, 0.01f);

            if (hit.collider != null)
            {
                body.AddForce(new Vector2(0f, jump));
            }
        }
    }
}
公共类玩家:单行为
{
公众浮标速度;
公众浮标跳跃;
公共游戏对象raycastPoint;//位于玩家下方0.01像素处
私人喷灌工;
私有刚体刚体;刚体刚体的重力比例尺=50
私人动画师;
私有void Start()
{
spriteender=GetComponent();
body=GetComponent();
animator=GetComponent

问题二

当我跳到左墙或右墙时,如果我一直按左键或右键,我的球员就不会再摔倒,而是靠在墙上

我试着将我的代码放入FixedUpdate方法(我知道这样更好),但我得到了相同的结果


我尝试将碰撞检测设置为Continuous,但结果相同。

请尝试以下代码解决第一个问题:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(BoxCollider2D))]
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Animator))]

public class Player_Controller : MonoBehaviour {
private Rigidbody2D body;
private bool canJump, facingRight;
private Animator anim;
[SerializeField]
private float moveSpeed, jumpForce;

void Start () 
{
    SetStartValues();
}

void FixedUpdate ()
{
    float horizontal = Input.GetAxis("Horizontal");
    animator.SetFloat("Speed", Mathf.Abs(horizontal));
    Flip(horizontal);
    Move(horizontal);
    Jump();
}

private void SetStartValues()
{
    body = GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();
    canJump = true;
    facingRight = true;
}

private void Jump()
{
    if (Input.GetKeyDown(KeyCode.Space) && canJump)
    {
        body.AddForce(new Vector2(0, jumpForce));
        canJump = false;
    }
}

private void Move(float x)
{
    body.velocity = new Vector2(x * moveSpeed * Time.deltaTime, body.velocity.y);
}

private void Flip(float x)
{
    if (x > 0 && !facingRight|| x < 0 && facingRight)
    {
        facingRight = !facingRight;
        transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y) ;
    }
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.tag == "Ground")
    {
        canJump = true;
    }
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
[RequiredComponent(类型(r2d))]
[RequiredComponent(typeof(Rigidbody2D))]
[RequiredComponent(typeof(Animator))]
公共类玩家\控制器:单行为{
私人刚体2D机构;
二等兵可以跳,面朝右;
私人动画师;
[序列化字段]
私人浮动速度、跳跃力;
无效开始()
{
设置起始值();
}
无效固定更新()
{
float horizontal=Input.GetAxis(“水平”);
动画师.SetFloat(“速度”,Mathf.Abs(水平));
翻转(水平);
移动(水平);
跳跃();
}
私有void SetStartValues()
{
body=GetComponent();
anim=GetComponent();
canJump=true;
facingRight=正确;
}
私有无效跳转()
{
if(Input.GetKeyDown(KeyCode.Space)和&canJump)
{
body.AddForce(新矢量2(0,跳跃力));
canJump=false;
}
}
私有无效移动(浮动x)
{
body.velocity=新矢量2(x*moveSpeed*Time.deltaTime,body.velocity.y);
}
专用空心翻转(浮动x)
{
如果(x>0&&!facingRight | | x<0&&facingRight)
{
facingRight=!facingRight;
transform.localScale=newvector2(transform.localScale.x*-1,transform.localScale.y);
}
}
专用空心OnCollisionInter2D(碰撞2D碰撞)
{
如果(collision.gameObject.tag==“地面”)
{
canJump=true;
}
}
}

不要忘了在地面物体上贴上“地面”标签。

关于你的问题二:这是一个常见问题。你对墙壁施加的力大于将玩家拉下来的重力。试着搜索2D Platformer,玩家粘在墙上,看看你是否能找到任何帮助。