- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,感谢您阅读本文。
我对 Unity 还很陌生,但不管怎样,我制作了一个小游戏,与马里奥和其他人的风格相同。一款你左右移动并跳跃来躲避敌人的游戏。
但是我遇到了一些代码问题,而且我似乎无法找到答案来帮助我解决它。
我需要检测玩家是否在屏幕中间向左或向右移动,这样我就知道相机是否需要向左或向右移动。
这是我迄今为止创建的相机脚本。
using UnityEngine;
using System.Collections;
public class CameraFunction : MonoBehaviour {
float ydir = 0f;
public GameObject player;
//for our GUIText object and our score
public GUIElement gui;
float playerScore = 0;
//this function updates our guitext object
void OnGUI(){
gui.guiText.text = "Score: " + ((int)(playerScore * 10)).ToString ();
}
//this is generic function we can call to increase the score by an amount
public void increaseScore(int amount){
playerScore += amount;
}
//Camera will be disabled when we load a level, set the score in playerprefs
void OnDisable(){
PlayerPrefs.SetInt ("Score",(int)(playerScore));
}
// Update is called once per frame
void Update () {
//check that player exists and then proceed. otherwise we get an error when player dies
if (player) {
if (player.transform.position.x > -1) {
//update our score every tick of the clock
playerScore += Time.deltaTime;
transform.position = new Vector3 (transform.position.x + 0.03f, transform.position.y, -10);
}
}
}
}
我的脚本仅检测“玩家”是否通过当前 View 的中间,然后开始移动。
希望您能理解我的意思,并能够帮助我。
更新:
如果你这样看,我们可以将屏幕分成 2 部分,即左侧和右侧部分,当然我们还有这 2 部分之间的中间部分。
人物/玩家从屏幕左侧开始,必须向右穿过 map 到达最终目标。
现在,当人经过屏幕中间并进入屏幕右侧时,相机将开始向右移动。但我无法让它检测到用户/玩家是否向后移动,那么他的相机必须“卡住”。
因此,如果玩家位置 > 屏幕的 50%(右侧)= 摄像机向右移动。如果玩家位置小于屏幕的 50%(左侧)= 摄像机“卡住”。
最佳答案
您可以在附加到相机的任何脚本组件中使用它:
if(camera.WorldToScreenPoint(player.transform.position).x > Screen.width / 2){
Debug.Log("Player is right of the middle of the screen.");
}
其中 WorldToScreenPoint
正在将世界坐标转换为屏幕坐标。
关于unity-game-engine - 检测 "player"是在屏幕中间的左边还是右边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26647949/
我正在开发一个需要在屏幕上查找对象的程序,到目前为止工作正常,但我遇到了多显示器配置问题。 GraphicsEnvironment.getLocalGraphicsEnvironment().getS
使用 mySql,我想列出客户对特定产品类别进行的所有购买。 所以,我有 3 个表:客户 (idCustomer, Name)、类别 (idCategory, CategoryName) 和订单 (i
我的网站上有一个关于 background-size:cover 的小问题我一直在 Firefox 中测试它,但是当我在谷歌浏览器中加载页面时,我在左边得到 1px 的白色。当我使用 backgrou
我已经搜索了几个小时来找到解决我的问题的方法,但没有成功。我遇到的问题是两个按钮的垂直堆叠。 这就是我想要做的:它说按钮在这里两次是我试图放置按钮的地方,但我所能做的就是让它们水平排列而不是垂直排列。
我有一个包含多个元素的导航栏。 Left1 Left2 Left3 Right1 Right2 Right3 我不知道如何将“fixedLef
您好,我正在尝试让 2 个 div 在左侧与右侧对齐。 #div1 #div2 #div1 #div2 #div3 #div2 #div3 #div3 诀窍是当浏览器窗口变小时,我希望#div2 位于
body { font-family: Arial, Helvetica, sans-serif; font-size: 13px;
这个问题在这里已经有了答案: In CSS Flexbox, why are there no "justify-items" and "justify-self" properties? (6
这是我的代码的 jsfiddle 链接: https://jsfiddle.net/Krisalay/zvxxeagh/ 我的 HTML 代码是: MESSAGE 1
所以我有 10 个复选框,每个标签都取自数组中相应的索引。我正在使用 ng-repeat 来展示它们: {{entity}}
我是一名优秀的程序员,十分优秀!