如何统一制作壁垒

时间:2019-11-19 08:49:51

标签: c# unity3d

我正试图像波斯王子的壁垒一样统一制造壁垒。

这个想法是,当您触摸墙壁并按“左移”时,播放器将运行几秒钟而不会掉落。 我想补充一点,如果您按“ enter”(输入),角色将使用Wall(墙)作为冲动来跳到一边。

您可以在视频的前几分钟观看: https://www.youtube.com/watch?v=zsnB7HEiLr0

我为角色控制器制作了这个脚本:

    public Transform playerPosition;
//controls the x movement. (right/left)
public float horizontalmove;
//controls the y movement. (forward/back)
public float verticalmove;
//controls the movement direction.
private Vector3 playerInput;
//Here I store my character controller.
public CharacterController player;
//controls the player speed.
public float playerSpeed;
//controls de movement direction according to camera 
public Vector3 movePlayer;
//controls the last movement
public Vector3 lastMovePlayer;

public float gravity = 9.8f;
public float fallVelocity;
public float jumpForce = 5.0f;
public float verticalSpeed;

private RaycastHit HitR;
private RaycastHit HitL;

//Here I store the main camera
public Camera mainCamera;
//It stores the camera direction when the player is looking forward.
private Vector3 camForward;
//It stores the camera direction when the player is looking right.
private Vector3 camRight;

//Checks
//The meaning of Caida is fall.
public bool Caida;
//The meaning of salto is jump.
public bool Salto;
public bool Wallrun;
public bool WallrunCount;

// Start is called befoe the first frame update
void Start()
{
    //i store the character controler.
    player = GetComponent<CharacterController>();

    Caida = true;
}

// Update is called once per frame
void Update()
{
    if (Wallrun == true)
    {
        Caida = false;
    }
    if (Salto == true)
    {
        fallVelocity -= gravity * Time.deltaTime;
        movePlayer = lastMovePlayer;
    }
    if (Caida == true)
    {
        fallVelocity -= gravity * Time.deltaTime;
    }
    if (player.isGrounded && Wallrun == false)
    {
        Caida = false;
        Salto = false;
        WallrunCount = false;
        //I assign the horizontal move to the w and s keys.
        horizontalmove = Input.GetAxis("Horizontal");

        //I assign the vertical move to the a and d keys.)
        verticalmove = Input.GetAxis("Vertical");

        //controls the movement direction
        playerInput = new Vector3(horizontalmove, 0, verticalmove);

        //limits the player speed. With this method if teh player waalks in diagonal doesn´t 
        //exceed the speed limit.
        playerInput = Vector3.ClampMagnitude(playerInput, 1);

        // It calls the function that give the camera look direction.
        camDirection();

        //Here, It`s calculates the player movement considering the camera point and the movement 
        //we have assing to teh player earlier
        //With this method the player always moves looking to the camera
        movePlayer = playerInput.x * camRight + playerInput.z * camForward;

        //The movement * the speed we want.
        movePlayer = movePlayer * playerSpeed;

        //we are going to say to the player where is looking at.
        player.transform.LookAt(player.transform.position + movePlayer);

        //It gives the gravity to the player.
        fallVelocity = -gravity * Time.deltaTime;
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Salto = true;
            fallVelocity = jumpForce;
        }
    }
    else if (!player.isGrounded && Salto == false && Wallrun == false)
    {
        Caida = true;
    }

    movePlayer.y = fallVelocity;

    //we give the movement to th eplayer.
    player.Move(movePlayer * Time.deltaTime);
    lastMovePlayer = movePlayer;
}

private void OnTriggerStay(Collider other)
{
    if (Input.GetKeyDown(KeyCode.LeftShift) && Wallrun == false && WallrunCount == false)
    {
        if (Input.GetKey("w"))
        {
            Wallrun = true;
            WallrunCount = true;
            fallVelocity = 5f;
            movePlayer.y = fallVelocity;
            movePlayer.z = movePlayer.z * 1.6f;
            if (Physics.Raycast(transform.position, transform.right, out HitR, 1))
            {
                movePlayer.x = 1.6f;
            }
            else if (Physics.Raycast(transform.position, -transform.right, out HitL, 1))
            {
                movePlayer.x = -1.6f;
            }
            StartCoroutine(wallrunTime());
        }
    }
}

void camDirection()
{
    //we store the forward and right directions here.
    camForward = mainCamera.transform.forward;
    camRight = mainCamera.transform.right;

    //we block the direction and the camera direction because we are not going to use it.
    camForward.y = 0;
    camRight.y = 0;

    //It gives as the normalized vectors.
    camForward = camForward.normalized;
    camRight = camRight.normalized;

}

private void OnControllerColliderHit(ControllerColliderHit hit)
{
    if (!player.isGrounded && hit.normal.y < 0.1f)
    {

        if (Input.GetKeyDown(KeyCode.Space))
        {
            fallVelocity = jumpForce;
            movePlayer = hit.normal * 7;
            player.transform.LookAt(player.transform.position + movePlayer);
        }
    }
}

IEnumerator wallrunTime()
{
    yield return new WaitForSeconds(1);
    Wallrun = false;
}

如您所见,当玩家进入左移时,它将检查角色向哪个方向移动,如果这是前面(w),则脚本进行z移动* 1.6(以使角色在墙中稍稍跑动),然后字符会在y轴上稍微向上一点。然后,脚本会检查墙壁是否在右侧或左侧,并根据墙壁的位置将字符粘贴到该墙壁上。

private void OnTriggerStay(Collider other)
{
    if (Input.GetKeyDown(KeyCode.LeftShift) && Wallrun == false && WallrunCount == false)
    {
        if (Input.GetKey("w"))
        {
            Wallrun = true;
            WallrunCount = true;
            fallVelocity = 5f;
            movePlayer.y = fallVelocity;
            movePlayer.z = movePlayer.z * 1.6f;
            if (Physics.Raycast(transform.position, transform.right, out HitR, 1))
            {
                movePlayer.x = 1.6f;
            }
            else if (Physics.Raycast(transform.position, -transform.right, out HitL, 1))
            {
                movePlayer.x = -1.6f;
            }
            StartCoroutine(wallrunTime());
        }
    }
}

通过这种方法,我可以使角色进入墙壁时跳跃,因为角色要撞到墙壁(在墙壁上反弹的条件)。

几秒钟后,角色掉下来模拟墙跑。

    IEnumerator wallrunTime()
{
    yield return new WaitForSeconds(1);
    Wallrun = false;
}

问题是,如果角色在与环境轴相同的方向上看时,使角色走墙,则效果很好。

当字符z轴与环境z轴方向相同时,它可以完美工作。但是,当轴不在同一方向时就是一场灾难。我给你看我录制的视频。

https://youtu.be/KH7rE9kh5d0

我想的问题是,使用我编写的代码,我正在根据环境的轴来移动角色,而不是根据环境的轴来移动角色,因此我必须告诉他根据他自己的方向来移动。

我的老师说我可能必须改变角色移动的方式。我想知道您是否可以在不更改移动方法的情况下告诉我解决该问题的方法,或者如果不可能,如何更改该移动。

谢谢。

0 个答案:

没有答案