统一对象仅在统一播放预览中朝一个方向移动
Unity object only moving in one direction in unity play preview
您好,我正在尝试学习统一性,并且我已经完成了一段代码,其中我需要对象在达到任何一侧的极限时上下移动并保持重复。但它只是向上轴,谁能告诉我这里出了什么问题?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shooter_movement : MonoBehaviour {
public float shootspeed = 2;
public bool turn = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (!turn)
{
transform.Translate (0, 2 * Time.deltaTime, 0);
}
else
{
transform.Translate (0, -2 * Time.deltaTime, 0);
}
}
void OncollisionEnter2D (Collision2D Collider){
Debug.Log ("Collision Works");
if (GetComponent<Collider>().gameObject.tag == "wall") {
if (turn) {
turn = false;
}
else {
turn = true;
}
}
}
}
首先,您选择了错误的游戏对象,您选择了自己的对象而不是接触的对象。
GetComponent<>() 将获取您的脚本附加到的游戏对象。
使用你的 collider 参数,它是触及你的对象。
这是正确的脚本和简化版
void OnCollisionEnter2D (Collision2D collider)
{
if (collider.gameObject.tag == "wall") {
turn = !turn;
}
}
您好,我正在尝试学习统一性,并且我已经完成了一段代码,其中我需要对象在达到任何一侧的极限时上下移动并保持重复。但它只是向上轴,谁能告诉我这里出了什么问题?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shooter_movement : MonoBehaviour {
public float shootspeed = 2;
public bool turn = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (!turn)
{
transform.Translate (0, 2 * Time.deltaTime, 0);
}
else
{
transform.Translate (0, -2 * Time.deltaTime, 0);
}
}
void OncollisionEnter2D (Collision2D Collider){
Debug.Log ("Collision Works");
if (GetComponent<Collider>().gameObject.tag == "wall") {
if (turn) {
turn = false;
}
else {
turn = true;
}
}
}
}
首先,您选择了错误的游戏对象,您选择了自己的对象而不是接触的对象。
GetComponent<>() 将获取您的脚本附加到的游戏对象。 使用你的 collider 参数,它是触及你的对象。
这是正确的脚本和简化版
void OnCollisionEnter2D (Collision2D collider)
{
if (collider.gameObject.tag == "wall") {
turn = !turn;
}
}