老师我的熊跑到十字路口就不见了
来自课程:萌熊跑酷-零基础精通跑酷游戏(第一季)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public CharacterController controller;
public float speed = 20f;
public float landDistance = 2.0f;
public int currentland = 0;
public float jumpHeight = 3.0f;
private Vector3 playerVelocity = new Vector3(0, 0, 0);
private float gravity = -9.88f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GetInput();
DoMove();
}
private void DoMove()
{
float targetX = currentland * landDistance;
Vector3 targetPos = new Vector3(targetX, transform.position.y, transform.position.z);
Vector3 moveDir = targetPos - transform.position;
controller.Move((moveDir + transform.forward) * speed * Time.deltaTime);
playerVelocity.y += playerVelocity.y + gravity * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
private void GetInput()
{
if (Input.GetKeyDown(KeyCode.W))
{
playerVelocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
else if (Input.GetKeyDown(KeyCode.S))
{
}
else if (Input.GetKeyDown(KeyCode.A))
{
MoveLand(false);
}
else if (Input.GetKeyDown(KeyCode.D))
{
MoveLand(true);
}
}
private void MoveLand(bool isRight)
{
currentland = currentland + (isRight ? 1 : -1);
currentland = Mathf.Clamp(currentland, -1, 1);
}
}

