一,设置
二,脚本
1,PlayerController
using System.Collections; using System.Collections.Generic; using UnityEngine;public class PlayerController : MonoBehaviour {private Rigidbody2D m_rg;public float MoveSpeed;public float JumpSpeed;//在角色下添加一个空物体//设置一个跳跃监测点public Transform CheckPoint;//设置一个跳跃监测半径public float CheckRadius;//设置一个跳跃监测层---角色与地面的检测public LayerMask WhatIsGround;//角色默认是否着地--truepublic bool isGround;private Animator Anim;//存储复活点的位置信息public Vector2 RespawnPosition;public LevelManager theLevel;void Start () {m_rg = gameObject.GetComponent();Anim = gameObject.GetComponent ();//游戏刚开始时,玩家的重生点,就是当前的初始位置点RespawnPosition = transform.position;theLevel = FindObjectOfType ();}// Update is called once per framevoid Update () {// isGround = Physics2D.OverlapCircle(CheckPoint.position, CheckRadius, WhatIsGround);//m_rg.gameObject.transform.rotation= Quaternion.identity;//------------------Input.GetAxisRaw没有小数值,只有整数,不会产生缓动------------------//角色水平移动//按住D键,判断如果大于0,则向右开始移动if (Input.GetAxisRaw("Horizontal") > 0){m_rg.velocity = new Vector2(MoveSpeed, m_rg.velocity.y);//设置自身缩放的值transform.localScale = new Vector2(1f,1f);}//角色水平移动//按住A键,判断如果小于0,则向左开始移动else if (Input.GetAxisRaw("Horizontal") < 0){m_rg.velocity = new Vector2(-MoveSpeed, m_rg.velocity.y);//如果new Vector2(-1f, 1f) x值为负数,则图片进行反转显示transform.localScale = new Vector2(-1f, 1f);}else//角色水平移动//松开按键,判断如果等于0,则停止移动 {m_rg.velocity = new Vector2(0, m_rg.velocity.y);}//角色按下空格键实现跳跃//禁止二连跳//要先判断角色是否在地面上,在地面上可以跳,不在地面上则不能跳if (Input.GetButtonDown("Jump")&& isGround){m_rg.velocity = new Vector2(m_rg.velocity.x,JumpSpeed);}Anim.SetFloat("Speed", m_rg.velocity.x);Anim.SetBool("Grouned", isGround);}private void OnTriggerEnter2D(Collider2D collision){if (collision.tag=="KillPlane"){//gameObject.SetActive(false);//使当前玩家的位置点为,保存的复活点位置//transform.position = RespawnPosition; theLevel.Respawn();}//角色与当前的复活点进行碰撞检测//把当前角色的位置信息,设置为重生的复活点if (collision.tag == "CheckPoint"){RespawnPosition = collision.transform.position;}}/// /// 实现角色跟随移动平台一起运动/// 把角色的父物体设置为移动平台/// /// private void OnCollisionEnter2D(Collision2D collision){Debug.Log("碰撞到的那个吊毛叫什么名字:"+ collision.gameObject.name);if (collision.gameObject.tag == "MovePlatForm"){transform.parent = collision.transform;Debug.Log("碰撞到的那个吊毛的父物体什么名字:" + transform.parent);}}private void OnCollisionExit2D(Collision2D collision){if (collision.gameObject.tag == "MovePlatForm"){transform.parent = null;}} }
2,MovePlatFromController
using System.Collections; using System.Collections.Generic; using UnityEngine;////// 平台移动脚本 /// public class MovePlatFromController : MonoBehaviour {//要移动的物体public GameObject PlatForm;//要移动物体开始的点public Transform StartPoint;//要移动物体结束的点public Transform EndPoint;//要移动物体的移动速度public float MoveSpeed;//要移动到的目标点public Vector2 target;void Start () {target = EndPoint.position;}// Update is called once per framevoid Update () {//把物体移动到指定的位置点上PlatForm.transform.position = Vector2.MoveTowards(PlatForm.transform.position, target, MoveSpeed*Time.deltaTime);//实现物体来回移动if (PlatForm.transform.position == EndPoint.position){target = StartPoint.position;}if (PlatForm.transform.position == StartPoint.position){target = EndPoint.position;}} }