我在学习 拾荒者 那个课程 现在 学到12课 player 和 outwall ,wall 发生碰撞
问题一 如果 刚体组件 得 is kinematic 点对勾 后 两个刚体 就可以重合 不会 相互排斥
问题二
using UnityEngine;
using System.Collections;
public class player : MonoBehaviour {
public float smoothing = 1;
public float restTime = 1;
private float restTimer = 0;
private Rigidbody2D rigidbody;
private BoxCollider2D collider;
private Animator animator;
private Vector2 targetPos = new Vector2(1,1);
// Use this for initialization
void Start () {
rigidbody = GetComponent<Rigidbody2D>();
collider = GetComponent<BoxCollider2D>();
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
rigidbody.MovePosition(Vector2.Lerp(transform.position, targetPos, smoothing * Time.deltaTime));
restTimer += Time.deltaTime;
if (restTimer < restTime) return;
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
if (h > 0) {
v = 0;
}
if (h != 0 || v != 0) {
//检测
collider.enabled = false;
RaycastHit2D hit = Physics2D.Linecast(targetPos, targetPos + new Vector2(h, v));
collider.enabled = true;
if (hit.transform == null)
{
targetPos += new Vector2(h, v);
//AudioManager.Instance.RandomPlay(step1Audio, step2Audio);
}
else
{
switch (hit.collider.tag)
{
case "OutWall":
break;
case "Wall":
break;
}
}
//targetPos += new Vector2(h, v);
}
}
}此段代码 执行 player 不能 移动了 不知道怎么回事 我都核对了好几遍 不知道哪里得 错误
还有 一些 代码颜色
private Rigidbody2D rigidbody;
private BoxCollider2D collider;
rigidbody collider; 下面 出现绿色得波浪线 不懂 是什么 意思
怎么插入图片呀
if (hit.transform == null)
{
targetPos += new Vector2(h, v);
//AudioManager.Instance.RandomPlay(step1Audio, step2Audio);
}
else
{
switch (hit.collider.tag)
{
case "OutWall":
break;
case "Wall":
break;
}
}
看看这个里面 是if执行了还是else执行了,在看看射线检测到了什么游戏物体
is kinematic 勾选上之后,刚体就不会在互相排斥了,这个是刚体自身的特性,不是问题
if (hit.transform == null)
{
targetPos += new Vector2(h, v);
Debug.Log("ASD");
//AudioManager.Instance.RandomPlay(step1Audio, step2Audio);
}
else
{
Debug.Log(hit.transform);
Debug.Log("ASD2");
switch (hit.collider.tag)
{
case "OutWall":
break;
case "Wall":
break;
}
}
输出是
player (UnityEngine.Transform)
UnityEngine.Debug:Log(Object)
player:Update() (at Assets/player.cs:51)
ASD2
UnityEngine.Debug:Log(Object)
player:Update() (at Assets/player.cs:52)
意思是不是是 player 碰撞到了 player
http://pan.baidu.com/s/1nvuSY1b 老师 我把源码 放在这里了 求助