我创建了一个Unity3d项目,我在对象后面使用了一些聚光灯来获取它们的阴影.我试图获得一次反映在地板上的阴影的真实尺寸(使用我的比例).有没有办法做到这一点?
我是品牌新Unity3D 5,我有我的第一个2D游戏,涉及碰撞检测的问题。我的移动物体是一个球,有 Rigidbody2D 和 CircleCollider2D。我的固定“碰撞器或触发器”是一个 BoxCollider 并附有一个脚本。OnTriggerEnter2D 应该在球穿过固定框时触发。我也试过 OnCollisionEnter2D 但我相当肯定我应该使用 OnTriggerEnter2D 因为我的固定框被标记为触发器。
我的代码:
public class LoseCollider : MonoBehaviour {
public LevelManager levelManager;
void OnCollisionEnter2D(Collision2D collision)
{
print("collide");
levelManager.LoadLevel("Lose");
}
void OnTriggerEnter2D(Collider2D trigger)
{
print("trigger");
levelManager.LoadLevel("Lose");
}
void OnCollisionEnter(Collision collision)
{
print("collide");
levelManager.LoadLevel("Lose");
}
void OnTriggerEnter(Collider trigger)
{
print("trigger");
levelManager.LoadLevel("Lose");
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在测试所有变体,但没有调用任何变体。这是我的对象的 Unity 属性:
和
我确定我错过了一些简单的东西,如果有人可以请向我指出。
我正在尝试将图像从 url 加载到 GameObject。
我找到了下一个教程:
https://www.youtube.com/watch?v=8UK2EsKBzv8
下载成功,但是看不到图片。
我究竟做错了什么?
// Use this for initialization
void Start () {
StartCoroutine(loadSpriteImageFromUrl("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/408px-Google_2015_logo.svg.png"));
}
IEnumerator loadSpriteImageFromUrl(string URL)
{
// Check internet connection
if (Application.internetReachability == NetworkReachability.NotReachable)
{
yield return null;
}
var www = new WWW(URL);
Debug.Log("Download image on progress");
yield return www;
if (string.IsNullOrEmpty(www.text))
{
Debug.Log("Download failed");
}
else
{
Debug.Log("Download succes");
Texture2D texture = new Texture2D(1, 1);
www.LoadImageIntoTexture(texture);
Sprite sprite = Sprite.Create(texture,
new Rect(0, 0, texture.width, texture.height),
Vector2.one / 2);
GetComponent<SpriteRenderer>().sprite = sprite; …Run Code Online (Sandbox Code Playgroud) 当我从编辑器运行游戏并保存加载数据时,我发现数据在:C:\Users\User\AppData\LocalLow\DefaultCompany\projectname\data.
当我构建它并获得可执行文件时,该数据仍可正常加载,但如果我保存并重新启动,则不会保存.但是当我从编辑器中启动它时它会这样做.
这是我的代码中的错误,还是我不从Unity Editor运行它的其他地方的文件?
稍后当我导出游戏以启动游戏时,我会拥有持久数据I Json文件,这些文件必须与游戏一起使用才能运行.
为清楚起见,这里是处理Json的保存/加载的类:
public class DataHandler
{
//Save Data
public static void saveData<T>(T dataToSave, string dataFileName)
{
string tempPath = Path.Combine(Application.persistentDataPath, "data");
tempPath = Path.Combine(tempPath, dataFileName + ".txt");
//Convert To Json then to bytes
string jsonData = JsonUtility.ToJson(dataToSave, true);
byte[] jsonByte = Encoding.ASCII.GetBytes(jsonData);
//Create Directory if it does not exist
if (!Directory.Exists(Path.GetDirectoryName(tempPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
}
//Debug.Log(path);
try
{
File.WriteAllBytes(tempPath, jsonByte);
Debug.Log("Saved Data to: " + tempPath.Replace("/", "\\"));
}
catch (Exception e)
{ …Run Code Online (Sandbox Code Playgroud) 我有txt包含游戏中地图数据的文件.问题是文件存储在中,Application.persistentDataPath所以我甚至可以从我的Android设备(创建的地图创建者)更改它,那么如何在我的PC上创建包含基本地图的txt文件,并在persistentDataPath安装时将其显示在我的Android设备上应用?
我想在x秒内和协程函数中将GameObject从位置A 移动到B. Vector3.MoveTowards我知道如何使用这个,Vector3.Lerp但这次更喜欢这样做,Vector3.MoveTowards因为两个函数的行为都不同.
有了Vector3.Lerp,这样做是这样的:
IEnumerator moveToX(Transform fromPosition, Vector3 toPosition, float duration)
{
float counter = 0;
//Get the current position of the object to be moved
Vector3 startPos = fromPosition.position;
while (counter < duration)
{
counter += Time.deltaTime;
fromPosition.position = Vector3.Lerp(startPos, toPosition, counter / duration);
yield return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我试图做同样的事情Vector3.MoveTowards,但它没有正常工作.问题是移动在x时间或持续时间之前结束.而且,它不能顺利移动.它跳到两个位置的中间而不是位置B的末尾.
这是Vector3.MoveTowards与上述问题一起使用的函数:
IEnumerator MoveTowards(Transform objectToMove, Vector3 toPosition, float duration)
{
float counter = 0;
while …Run Code Online (Sandbox Code Playgroud) 我对 Unity 很陌生,所以这可能是一个愚蠢的问题,但是在检查器中设置值是否与在代码中设置默认值相同?
例子:
public string name; //then go to inspector and set it as "Bob".
Run Code Online (Sandbox Code Playgroud)
或者
public string name = "Bob";
Run Code Online (Sandbox Code Playgroud)
那么,换个说法,上面的这些动作会达到同样的效果吗?
我为 PC 和移动设备(如 android、ios)创建了游戏。我在屏幕上添加了一些游戏组件来帮助移动用户导航播放器。
问题是游戏手柄也出现在 PC 版本上。
如何隐藏此类组件,使其不显示在游戏的非移动平台上。
我有自定义的构建脚本,我想在其中添加更多选项。目标架构是其中之一。(我将IL2CPP用作脚本后端-我通过脚本进行设置)
根据文档,我可以使用PlayerSettings.SetArchitecture(),但似乎根本不适合我。它只有0-无,1-ARM64,2-通用的选项,我想要全部。所以我尝试使用:
PlayerSettings.SetArchitecture(BuildTargetGroup.Android, unchecked((int)AndroidArchitecture.All));
Run Code Online (Sandbox Code Playgroud)
但它不会改变任何东西,下面的几行也不会改变任何东西:
PlayerSettings.SetArchitecture(BuildTargetGroup.Android, unchecked((int)AndroidArchitecture.ARM64));
PlayerSettings.SetArchitecture(BuildTargetGroup.Android, unchecked((int)AndroidArchitecture.ARMv7));
PlayerSettings.SetArchitecture(BuildTargetGroup.Android, unchecked((int)AndroidArchitecture.X86));
Run Code Online (Sandbox Code Playgroud)
我想知道如何正确地做到这一点?
我有一个点(A)和一个向量(V)(假设它是无限长),并且我想找到直线上与原始点(A)最接近的点(B)。使用Unity Vector2或Vector3的最简单的表达式是什么?