C# 我只想将同一预制件的每个实例夹紧到y轴
我实例化了预制的C# 我只想将同一预制件的每个实例夹紧到y轴,c#,unity3d,mobile,C#,Unity3d,Mobile,我实例化了预制的桨两次,然后尝试仅使用Mathf.Clamp将其夹紧到y轴 但是,最小值和最大值最终应用于桨的两个实例,因此它们可以在x轴上移动 好的,我正在尝试制作一个2d移动乒乓球游戏,我有一个预制的桨,球与之碰撞的球拍/球拍 我在屏幕的每一侧实例化了两次相同的预置: Paddle paddle1 = Instantiate(Paddle) as Paddle; Paddle paddle2 = Instantiate(Paddle) as Paddle; paddle2.Init(true
桨
两次,然后尝试仅使用Mathf.Clamp
将其夹紧到y轴
但是,最小值和最大值最终应用于桨的两个实例,因此它们可以在x轴上移动
好的,我正在尝试制作一个2d移动乒乓球游戏,我有一个预制的桨,球与之碰撞的球拍/球拍
我在屏幕的每一侧实例化了两次相同的预置:
Paddle paddle1 = Instantiate(Paddle) as Paddle;
Paddle paddle2 = Instantiate(Paddle) as Paddle;
paddle2.Init(true); //right paddle
paddle1.Init(false); //left paddle
现在,我希望每个桨只能在y轴上移动(上下)
所以我试着用:
var poss = transform.position;
poss.x = Mathf.Clamp(transform.position.x, -1.0f, 1.0f);
transform.position = poss;
然而,这导致左桨仍然能够向右移动,右桨能够向左移动(我根本不希望它们在轴上移动)
这是Gamemanager.cs文件:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager: MonoBehaviour
{
public Ball Ball;
public Paddle Paddle;
public static Vector2 topRight;
public static Vector2 bottomLeft;
// Start is called before the first frame update
void Start()
{
bottomLeft = Camera.main.ScreenToWorldPoint(new Vector2 (0,0));
topRight = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width,Screen.height));
Instantiate(Ball);
//Paddle paddle1 = Instantiate(Paddle) as Paddle;
Paddle paddle1 = Instantiate(Paddle) as Paddle;
Paddle paddle2 = Instantiate(Paddle) as Paddle;
paddle2.Init(true); //right paddle
paddle1.Init(false); //left paddle
}
void Update()
{
}
}
这是文件pable.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Paddle : MonoBehaviour
{
Rigidbody2D rb;
float directionY;
float speed;
float height;
float distance;
string input;
bool isRight;
// Start is called before the first frame update
void Start()
{
height = transform.localScale.y;
speed=5f;
}
public void Init(bool isRightPaddle)
{
isRight = isRightPaddle;
Vector2 pos = Vector2.zero;
if(isRightPaddle)
{
//Place paddle on the right of the screen
pos = new Vector2(GameManager.topRight.x, 0);
pos -= Vector2.right * transform.localScale.x; //Move a bit to the left
}
else
{
//Place paddle on the left of the screen
pos = new Vector2(GameManager.bottomLeft.x, 0);
pos += Vector2.right * transform.localScale.x; //Move a bit to the right
}
//Update this paddle's position
transform.position = pos;
}
void Update()
{
var poss = transform.position;
poss.x = Mathf.Clamp(transform.position.x, -8.0f, 8.0f);
transform.position = poss;
}
}
我希望每个桨都不能在x轴上移动。只有在y轴中,黑客才能将刚体添加到预设中,删除刚体组件默认使用的任何重力修改器。然后有一个勾选框,您可以勾选它来冻结游戏对象的x位置,这将为您提供所需的结果。如果您想完全冻结轴而不是夹紧它,您应该执行以下操作
private float xPosition;
...
public void Init(bool isRightPaddle)
{
...
xPosition = pos.x;
}
然后继续重置到X中的该位置
// is executed after all Update methods are done
// so more or less the last thing before the frame is renderd
private void LateUpdate()
{
transform.position = new Vector2(xPosition, transform.position.y);
}
或者——因为有一个Rigidbody2D
参与其中——而是在
private void FixedUpdate()
{
rb.position = new Vector2(xPosition, rb.position.y);
}
但如果你根本不想让它们移动,为什么要在X方向进行所有的计算和夹紧呢?只需存储原始X位置,并在LateUpdate
或FixedUpdate
中的每一帧将其写回。我不知道如何找到确切的X位置。。你不在Start
/Init
中计算一个吗?不,你引用的是哪个脚本?patle.cs
->pos=new Vector2(GameManager.topRight.x,0);pos-=Vector2.right*transform.localScale.x代码>例如?这两个都不起作用,,他们仍在移动xok再次尝试第一个起作用!非常感谢你的帮助