2015-09-24 3 views
0

Так вот проблема в виде видеоTransform перетаскиванием на движение

https://pbs.twimg.com/tweet_video/CPsvjgbWoAACKCp.mp4

Когда не перемещая корабль на щит движется вокруг просто отлично, но когда вы начинаете двигаться он может тянуть только за вами, и немного двигаться бит, который просто не годится. Я не могу понять, что я сделал неправильно здесь!

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); 

    } 
} 

Спасибо миллион за любую помощь :)

+0

Посмотрите на функцию 'RotateAround()', она может лучше соответствовать вашим целям: http://docs.unity3d.com/ScriptReference/Transform.RotateAround.html – Eraph

+0

Я попробовал RotateAround(), но это только кажется вращение экрана на его собственной оси в указанном месте, где мне нужно изменить положение трансформации –

ответ

0

Вы должны переместить щит вместе с кораблем. Вы можете сделать это, добавив скорость корабля к экрану. Что-то вроде:

shipPos = target.position; 
transform.position += shipVelocity; 
separation = Mathf.Abs((transform.position - shipPos).magnitude); 

Или сделать щит ребенок объект корабля.

+0

Это действительно не приносит пользы :( –

+0

Уход за щитом работал после того, как я избавился от условного утверждения вокруг «transform.rotation» больше вы знаете! lol –

0

ОК, так что все, что мне нужно было сделать, это сделать щит дочерним и избавиться от условного утверждения вокруг transform.rotation, чтобы он не наследовал родительский поворот и не разрушил экран. Это мелочи!

Смежные вопросы