C# 移动时拖动变换
这就是视频形式的问题 当不移动飞船时,护盾可以很好地移动,但当你开始移动时,护盾只能拖到你身后,移动一点,这一点都不好。我不知道我做错了什么C# 移动时拖动变换,c#,unity3d,transform,C#,Unity3d,Transform,这就是视频形式的问题 当不移动飞船时,护盾可以很好地移动,但当你开始移动时,护盾只能拖到你身后,移动一点,这一点都不好。我不知道我做错了什么 public class ShieldMovement : MonoBehaviour { public Transform target; //player shield is attaced to public float circSpeed = 0.1f; // Speed Shield moves around ship private Ve
public class ShieldMovement : MonoBehaviour {
public Transform target; //player shield is attaced to
public float circSpeed = 0.1f; // Speed Shield moves around ship
private Vector3 direction = Vector3.up;
private float distance = 0.8f; // distance from player so it doesn't clip
private float deadzone = 0.15f;
private float separation;
private bool canMove = true;
private Vector3 shipPos;
private Rigidbody2D tRB;
private Rigidbody2D rb;
// Use this for initialization
void Start ()
{
tRB = target.GetComponent<Rigidbody2D>();
rb = GetComponent<Rigidbody2D>();
}
void OnCollisionEnter2D (Collision2D other)
{
canMove = false;
}
void OnCollisionStay2d(Collision2D other)
{
canMove = false;
}
void OnCollisionExit2D (Collision2D other)
{
canMove = true;
}
// Update is called once per frame
void Update () {
float rh = Input.GetAxisRaw("rightH");
float rv = Input.GetAxisRaw("rightV");
shipPos = target.position;
separation = Mathf.Abs((transform.position - shipPos).magnitude);
if(Mathf.Abs(rh) > deadzone || Mathf.Abs(rv) > deadzone)
{
Vector3 targetDir = new Vector3(rh, rv, 0.0f);
direction = Vector3.Slerp(transform.position-shipPos, targetDir, circSpeed);
}
Ray ray = new Ray(shipPos, direction); // cast ray in direction of point on circle shield is to go
if(canMove)
{
transform.position = ray.GetPoint(distance); //move shield to the ray as far as the distance
}
if(separation != distance) //If the shield get torn away from the ship
{
transform.position = ray.GetPoint(distance); //move shield to the ray as far as the distance
}
float angleY = transform.position.y - shipPos.y;
float angleX = -(transform.position.x - shipPos.x);
float angle = Mathf.Atan2 (angleY, angleX) * Mathf.Rad2Deg-90; //Get angle
if(Mathf.Abs(rh) > deadzone || Mathf.Abs(rv) > deadzone)
{
transform.rotation = Quaternion.AngleAxis(angle,Vector3.forward*-1); // keep shield facing outwards in respect to player
}
}
void FixedUpdate ()
{
if(!canMove)
{
tRB.velocity = new Vector2(0.0f, 0.0f);
rb.velocity = new Vector2(0.0f, 0.0f);
}
}
公共类屏蔽运动:单一行为{
public Transform target;//附加玩家盾牌
公众浮标circSpeed=0.1f;//速度盾在船舶周围移动
专用矢量3方向=矢量3.up;
私有浮动距离=0.8f;//与玩家的距离,因此不会剪辑
私人浮动死区=0.15f;
私人浮动分离;
private bool canMove=true;
私人向量机3 shipPos;
私人刚体2d tRB;
私有刚体2d rb;
//用于初始化
无效开始()
{
tRB=target.GetComponent();
rb=GetComponent();
}
空心OnCollisionInter2D(碰撞2D其他)
{
canMove=false;
}
无效碰撞Stay2D(碰撞2D其他)
{
canMove=false;
}
空心OnCollisionExit2D(碰撞2D其他)
{
canMove=true;
}
//每帧调用一次更新
无效更新(){
float rh=Input.GetAxisRaw(“rightH”);
float rv=Input.GetAxisRaw(“rightV”);
shipPos=目标位置;
分离=数学绝对值((变换位置-shipPos).magnity);
如果(主Abs(右)>死区| |主Abs(右)>死区)
{
Vector3 targetDir=新Vector3(右、右、0.0f);
方向=矢量3.Slerp(transform.position-shipPos、targetDir、circSpeed);
}
射线=新射线(shipPos,方向);//按圆盾上点的方向投射射线
如果(可以移动)
{
transform.position=ray.GetPoint(距离);//将遮罩尽可能远地移动到光线
}
if(分离!=距离)//如果防护罩从船上撕下
{
transform.position=ray.GetPoint(距离);//将遮罩尽可能远地移动到光线
}
浮动角度y=变换位置y-发货位置y;
浮动角度x=-(transform.position.x-shipPos.x);
浮动角度=Mathf.Atan2(角度Y,角度X)*Mathf.Rad2Deg-90;//获取角度
如果(主Abs(右)>死区| |主Abs(右)>死区)
{
transform.rotation=Quaternion.AngleAxis(角度,矢量3.向前*-1);//保持盾牌相对于玩家朝外
}
}
无效固定更新()
{
如果(!可以移动)
{
tRB.velocity=新矢量2(0.0f,0.0f);
rb.velocity=新矢量2(0.0f,0.0f);
}
}
非常感谢您的帮助:)您需要将护盾与船一起移动。您可以通过将船速添加到护盾中来完成此操作。 比如:
shipPos = target.position;
transform.position += shipVelocity;
separation = Mathf.Abs((transform.position - shipPos).magnitude);
或者将盾牌作为飞船的子对象。好的,我所需要做的就是将盾牌作为子对象,并去掉
变换.rotation
周围的条件语句,这样它就不会继承父旋转并破坏盾牌。这是一些小事情!看看RotateAround()
function,它可能更适合您的目的:我尝试了RotateAround(),但它似乎只在给定的位置绕着自己的轴旋转屏蔽,在这里我需要更改变换位置。在我去掉围绕“transform.rotation”的条件语句后,屏蔽开始工作!哈哈