Unity 2D物体移动
Posted yueqingli
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 2D物体移动相关的知识,希望对你有一定的参考价值。
一,设置
二,脚本
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; //角色默认是否着地--true public bool isGround; private Animator Anim; //存储复活点的位置信息 public Vector2 RespawnPosition; public LevelManager theLevel; void Start () { m_rg = gameObject.GetComponent<Rigidbody2D>(); Anim = gameObject.GetComponent<Animator>(); //游戏刚开始时,玩家的重生点,就是当前的初始位置点 RespawnPosition = transform.position; theLevel = FindObjectOfType<LevelManager>(); } // Update is called once per frame void 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; } } /// <summary> /// 实现角色跟随移动平台一起运动 /// 把角色的父物体设置为移动平台 /// </summary> /// <param name="collision"></param> 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; /// <summary> /// 平台移动脚本 /// </summary> 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 frame void 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; } } }
以上是关于Unity 2D物体移动的主要内容,如果未能解决你的问题,请参考以下文章