C# Unity2D:如何与屏幕侧面碰撞
我目前正在为Android开发一款2D游戏。在我的场景中有一个播放器,如果用户倾斜他的设备,播放器对象将在地面上移动。但他只是从屏幕的左右两侧移出。我试图筑起一堵“墙”,但没有成功。在我的玩家Gameobject上有一个边缘碰撞器。现在我的问题是:我的玩家游戏对象如何与屏幕侧面碰撞 这是我的代码:C# Unity2D:如何与屏幕侧面碰撞,c#,android,unity3d,collider,C#,Android,Unity3d,Collider,我目前正在为Android开发一款2D游戏。在我的场景中有一个播放器,如果用户倾斜他的设备,播放器对象将在地面上移动。但他只是从屏幕的左右两侧移出。我试图筑起一堵“墙”,但没有成功。在我的玩家Gameobject上有一个边缘碰撞器。现在我的问题是:我的玩家游戏对象如何与屏幕侧面碰撞 这是我的代码: public GameObject player; // Use this for initialization void Start () {
public GameObject player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 dir = Vector3.zero;
dir.y = Input.acceleration.x;
player.transform.Translate(new Vector2(dir.y, 0) * Time.deltaTime * 2000f);
}
非常感谢!:)
七月
编辑:
图1是我的墙,图2是我的球员的
我想用屏幕旁边的墙来解决这个问题。这些是我的照片
解决了的 解决方案代码:
Vector3 position = player.transform.position;
translation = Input.acceleration.x * movementSpeed * 50f;
if (player.transform.position.x + translation < LeftlimitScreen)
{
position.x = -LeftlimitScreen;
}
else if(transform.position.x + translation > RightlimitScreen)
{
position.x = RightlimitScreen;
}
else
{
position.x += translation;
player.transform.position = position;
}
Vector3 position=player.transform.position;
平移=输入.加速度.x*移动速度*50f;
if(player.transform.position.x+translationRightlimitScreen)
{
位置x=右侧限制屏幕;
}
其他的
{
位置x+=平移;
player.transform.position=位置;
}
此代码适用于我!:) 您可以在场景中,使用对撞机将2个空游戏对象放置在设备中显示的区域之外,以便玩家与它们碰撞 您还可以通过代码限制玩家可以移动的边界。使用Mathf.Clamp()应用此方法,然后需要在场景的x坐标中设置边界 您将看到,我们使用刚体,而不是使用其变换修改播放器的位置
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
5.0f
);
}
}
您可以在此处查看整个教程:
更新其他选项:
//You select here the speed you consider
float speed = 1.0f;
void Update () {
Vector3 dir = Vector3.zero;
float InputValue = Input.acceleration.x * speed;
//You need to set the values for this limits (max and min) based on your scene
dir.y = Mathf.Clamp(InputValue, 0.5f, 50.5f);
player.transform.position = dir;
}
更新2:
没有钳制,只需设置脚本的限制
void Update () {
Vector3 position = player.transform.position ;
translation = Input.acceleration.x * speed;
if( player.transform.position.y + translation < leftLimitScreen )
position.y = -leftLimitScreen ;
else if( myTransform.position.x + translation > rightLimitScreen )
position.y = rightLimitScreen ;
else
position.y += translation ;
player.transform.position = position ;
}
void更新(){
Vector3位置=player.transform.position;
平移=输入加速度x*速度;
if(player.transform.position.y+translationrightLimitScreen)
位置y=右侧限制屏幕;
其他的
位置y+=平移;
player.transform.position=位置;
}
您可以在场景中,在设备中将显示的区域之外放置2个带有碰撞器的空游戏对象,以便玩家与它们碰撞
您还可以通过代码限制玩家可以移动的边界。使用Mathf.Clamp()应用此方法,然后需要在场景的x坐标中设置边界
您将看到,我们使用刚体,而不是使用其变换修改播放器的位置
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
5.0f
);
}
}
您可以在此处查看整个教程:
更新其他选项:
//You select here the speed you consider
float speed = 1.0f;
void Update () {
Vector3 dir = Vector3.zero;
float InputValue = Input.acceleration.x * speed;
//You need to set the values for this limits (max and min) based on your scene
dir.y = Mathf.Clamp(InputValue, 0.5f, 50.5f);
player.transform.position = dir;
}
更新2:
没有钳制,只需设置脚本的限制
void Update () {
Vector3 position = player.transform.position ;
translation = Input.acceleration.x * speed;
if( player.transform.position.y + translation < leftLimitScreen )
position.y = -leftLimitScreen ;
else if( myTransform.position.x + translation > rightLimitScreen )
position.y = rightLimitScreen ;
else
position.y += translation ;
player.transform.position = position ;
}
void更新(){
Vector3位置=player.transform.position;
平移=输入加速度x*速度;
if(player.transform.position.y+translationrightLimitScreen)
位置y=右侧限制屏幕;
其他的
位置y+=平移;
player.transform.position=位置;
}
在一个原型中,我创建的解决方案是使用边界中没有精灵的对象创建“墙”,并使用以下脚本检查Raycast是否有内容:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
RaycastHit2D[] hit;
Vector2[] directions;
private Vector2 targetPosition;
private float moveSpeed;
private float moveHDir;
private float wallPos;
private bool hitLeft;
private bool hitRight;
// Use this for initialization
void Start () {
directions = new Vector2[2] {Vector2.right, Vector2.left};
hitLeft = false;
hitRight = false;
}
// Update is called once per physics timestamp
void FixedUpdate () {
foreach (Vector2 dir in directions) {
hit = Physics2D.RaycastAll(transform.position, dir);
Debug.DrawRay(transform.position, dir);
if (hit[1].collider != null) {
// Keyboard control
if (Input.GetAxisRaw("Horizontal") != 0) {
moveHDir = Input.GetAxisRaw("Horizontal");
// I have found that a 5% of the size of the object it's a
// good number to set as a minimal distance from the obj to the borders
if (hit[1].distance <= (transform.localScale.x * 0.55f)) {
if (dir == Vector2.left) {
hitLeft = true;
} else {
hitRight = true;
}
wallPos = hit[1].collider.transform.position.x;
// Condition that guarantee that the paddle do not pass the borders of the screen
// but keeps responding if you try to go to the other side
if ((wallPos > this.transform.position.x && moveHDir < 0) ||
(wallPos < this.transform.position.x && moveHDir > 0)) {
moveSpeed = gControl.initPlayerSpeed;
} else {
moveSpeed = 0;
}
} else {
if (dir == Vector2.left) {
hitLeft = false;
} else {
hitRight = false;
}
if (!hitRight && !hitLeft)
{
moveSpeed = gControl.initPlayerSpeed;
}
}
}
}
}
targetPosition = new Vector2((transform.position.x + (moveSpeed * moveHDir)), transform.position.y);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家控制器:单行为{
RaycastHit2D[]命中;
向量2[]方向;
专用矢量2目标位置;
私人浮动速度;
私人浮动利率;
私人流动电话;
二等兵布尔·希特勒;
私人布尔希特勒;
//用于初始化
无效开始(){
方向=新向量2[2]{Vector2.right,Vector2.left};
hitLeft=false;
正确=错误;
}
//每个物理时间戳调用一次更新
无效固定更新(){
foreach(方向向量2 dir){
hit=Physics2D.RaycastAll(transform.position,dir);
Debug.DrawRay(transform.position,dir);
if(命中[1]。碰撞器!=null){
//键盘控制
if(Input.GetAxisRaw(“水平”)!=0){
moveHDir=Input.GetAxisRaw(“水平”);
//我发现物体大小的5%是
//要设置为从obj到边界的最小距离的好数字
if(点击[1]。距离this.transform.position.x&&moveHDir<0)||
(wallPos0){
移动速度=gControl.initPlayerSpeed;
}否则{
移动速度=0;
}
}否则{
if(dir==Vector2.左){
hitLeft=false;
}否则{
正确=错误;
}
如果(!hitRight&!hitLeft)
{
移动速度=gControl.initPlayerSpeed;
}
}
}
}
}
targetPosition=新矢量2((transform.position.x+(moveSpeed*moveHDir)),tr