刚体与碰撞体
前面弄了瓦片地图。
但此时显示出来的东西本质上还是图片。
怎么让图片能成为独立的物体?
需要添加两个部分,一个是让静态的图片能够模拟物理效果,各类刚体。
另一个让静态的图片能够模拟碰撞效果,各类碰撞体。
特别的,对瓦片地图,需要选用针对性的碰撞体,这样的好处是unity自动划分好碰撞区域,无需手动一个一个添加。
控制移动
在edit菜单栏里有一项project setting,管理input
①获取这个输入管理中的设定值,使用 Input.GetAxis(“Horizontal”); 函数返回值是-1(水平向左),0(没动),+1(水平向右),返回值是一个区间,-1到0,0到+1,所以是浮点数。
②根据返回值判断方向,移动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerContral : MonoBehaviour
{
public Rigidbody2D rd;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Move();
}
void Move()
{
float hori;
hori= Input.GetAxis("Horizontal");
if (hori!=0)
{
rd.velocity = new Vector2(hori * 1, rd.velocity.y);
}
}
}
移动流程和我想象的不一样,首先它不是利用transform来移动,而是用的刚体的velocity属性,区别在于后者移动时能模拟物理效果(重力,摩擦力);特别的velocity属性用的是直接赋值的方法;其次它判断按键不是用的类似 Input.GetKey(KeyCode.RightArrow);而是直接读取输入管理设定值。
运行效果:
为什么会这样? 因为在刚体设置里没有禁止z轴旋转
假设感觉移动速度慢了,还可以添加一个速度参数
rd.velocity = new Vector2(hori *speed, rd.velocity.y);
控制图片方向
原理是变换组件下的缩放scale参数,当x=-1时,图片会“翻面”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerContral : MonoBehaviour
{
public Rigidbody2D rd;
public float speed;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Move();
}
void Move()
{
float hori = Input.GetAxis("Horizontal"); ;
//和上句区别在于,后者能把-1到0的区间直接换成-1,即facedirection的值只能是-1,0,+1
float facedirection = Input.GetAxisRaw("Horizontal");
if (hori != 0)
{
rd.velocity = new Vector2(hori * speed, rd.velocity.y);
}
if (facedirection != 0)
{
transform.localScale = new Vector3(facedirection, 1, 1);
}
}
}
可以加上平滑运动
rd.velocity = new Vector2(hori * speed*Time.deltaTime, rd.velocity.y);
此时需要调整speed的值,大约80比较好。
添加跳跃
依葫芦画瓢,jump仍然是读取的输入管理,而不是直接硬编码。
GetButtonDown还有个好处,它只触发一次,哪怕是按住不放,不会出现按住不放,人物就一直飞上天。
连续跳,那就是多按几次,但是缺点是人物就一直飞上天。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerContral : MonoBehaviour
{
public Rigidbody2D rd;
public float speed;
public float jumpforce;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Move();
}
void Move()
{
float hori = Input.GetAxis("Horizontal"); ;
//和上句区别在于,后者能把-1到0的区间直接换成-1,即facedirection的值只能是-1,0,+1
float facedirection = Input.GetAxisRaw("Horizontal");
if (hori != 0)
{
rd.velocity = new Vector2(hori * speed*Time.deltaTime, rd.velocity.y);
}
if (facedirection != 0)
{
transform.localScale = new Vector3(facedirection, 1, 1);
}
if (Input.GetButtonDown("Jump"))
{
rd.velocity = new Vector2(rd.velocity.x, jumpforce * Time.deltaTime);
}
}
}
添加动画
①首先给物体添加Animator组件
②在window菜单中打开Animation窗口,把多张图片拖拽进去,不要忘记修改pixels per unit
感觉动画太快了,调整samples。
添加其他的动画:
③利用Animator窗口来管理若干个动画
怎么判断它会在idel和run两个动画之间切换?
添加参数
添加条件,如果跑动速度大于0.1
取消动画的退出时间,让它立刻转换,
④ 怎么设置runing这个参数? 需要在代码中编写,也就是说管理整个动画的切换,一部分是依靠animator窗口,一部分是依靠代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerContral : MonoBehaviour
{
public Rigidbody2D rd;
public float speed;
public float jumpforce;
public Animator anim;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Move();
}
void Move()
{
float hori = Input.GetAxis("Horizontal"); ;
//和上句区别在于,后者能把-1到0的区间直接换成-1,即facedirection的值只能是-1,0,+1
float facedirection = Input.GetAxisRaw("Horizontal");
//角色水平移动
if (hori != 0)
{
rd.velocity = new Vector2(hori * speed*Time.deltaTime, rd.velocity.y);
//配合Animator窗口,设置参数runing的值,无论向左向右,绝对值都为1,即runing的值为1
//runing的值为1,满足Animator窗口设定的切换条件
anim.SetFloat("runing",Mathf.Abs(facedirection));
}
if (facedirection != 0)
{
transform.localScale = new Vector3(facedirection, 1, 1);
}
//角色跳跃
if (Input.GetButtonDown("Jump"))
{
rd.velocity = new Vector2(rd.velocity.x, jumpforce * Time.deltaTime);
}
}
}