2014-10-15 4 views
0

안녕하세요 다시 StackOverflow, 나는 현재 내 탄도 아크 코드로 고심하고있다. 내 문제는 아크가 너무 약해서 발사체가 거의 멀리 떨어져 바닥에 떨어지는 것을 의미합니다. 네가 나에게 줄 수있는 도움은 크게 감사 할 것이다.2D 탄도 아크 너무 약함 Unity

관련 코드 :

using UnityEngine; 
using System.Collections; 

public class ProjectileDrag : MonoBehaviour { 

//variables for catapult 
public float maxStretch = 3.0f; 
public LineRenderer catapultLineFront; 
public LineRenderer catapultLineBack; 

private SpringJoint2D spring; 
private Transform catapult; 
private Ray rayToMouse; 
private Ray leftCatapultToProjectile; 
private float maxStretchSqr; 
private float circleRadius; 
private Vector2 prevVelocity; 
private bool clickedOn = false; 

//prediction line 
int samples = 15; 
float spacing = 0.1f; 
private GameObject[] line; 
private Vector3 home; 
int force = 4; 



void Awake(){ 
    spring = GetComponent<SpringJoint2D>(); 
    catapult = spring.connectedBody.transform; 
} 

// Use this for initialization 
void Start() { 
    LineRendererSetup(); 
    rayToMouse = new Ray (catapult.position, Vector3.zero); 
    leftCatapultToProjectile = new Ray (catapultLineFront.transform.position, Vector3.zero); 
    maxStretchSqr = maxStretch * maxStretch; 
    CircleCollider2D circle = collider2D as CircleCollider2D; 
    circleRadius = circle.radius; 

    line = new GameObject[samples]; 
    for (int i = 0; i < line.Length; i++){ 
     var go = GameObject.CreatePrimitive(PrimitiveType.Sphere); 
     go.collider.enabled = false; 
     go.transform.localScale = new Vector3(0.2f,0.2f,0.2f); 
     line[i] = go; 
    } 

    home = transform.position; 
} 

void FixedUpdate(){ 

    if (clickedOn) 
      DisplayLine(); 
} 


// Update is called once per frame 
void Update() { 

    if (clickedOn) 
      Dragging(); 

    if (spring != null) { 
     if(!rigidbody2D.isKinematic && prevVelocity.sqrMagnitude > rigidbody2D.velocity.sqrMagnitude){ 
      Destroy (spring); 
      rigidbody2D.velocity = prevVelocity; 
     } 
     if (!clickedOn) 
      prevVelocity = rigidbody2D.velocity; 

     LineRendererUpdate(); 
      } else { 
     catapultLineFront.enabled = false; 
     catapultLineBack.enabled = false; 
      } 

} 

void LineRendererSetup(){ 
    catapultLineFront.SetPosition (0, catapultLineFront.transform.position); 
    catapultLineBack.SetPosition (0, catapultLineBack.transform.position); 


    catapultLineFront.sortingLayerName = "Foreground"; 
    catapultLineBack.sortingLayerName = "Foreground"; 


    catapultLineFront.sortingOrder = 3; 
    catapultLineBack.sortingOrder = 1; 

} 

void OnMouseDown(){ 
    spring.enabled = false; 
    clickedOn = true; 
} 

void OnMouseUp(){ 
    spring.enabled = true; 
    rigidbody2D.isKinematic = false; 
    clickedOn = false; 
} 

void Dragging(){ 
    Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition); 
    Vector2 catapultToMouse = mouseWorldPoint - catapult.position; 

    if (catapultToMouse.sqrMagnitude > maxStretchSqr) { 
      rayToMouse.direction = catapultToMouse; 
     mouseWorldPoint = rayToMouse.GetPoint(maxStretch); 
      } 

    mouseWorldPoint.z = 0.0f; 
    transform.position = mouseWorldPoint; 

} 

void LineRendererUpdate(){ 

    Vector2 catapultToProjectile = transform.position - catapultLineFront.transform.position; 
    leftCatapultToProjectile.direction = catapultToProjectile; 
    Vector3 holdPoint = leftCatapultToProjectile.GetPoint (catapultToProjectile.magnitude + circleRadius); 
    catapultLineFront.SetPosition (1, holdPoint); 
    catapultLineBack.SetPosition (1, holdPoint); 

} 

void DisplayLine(){ 
    line [0].transform.position = transform.position; 
    Vector3 v3 = transform.position; 
    float y = (force * (home - transform.position)).y; 
    float t = 0.0f; 
    v3.y = 0.0f; 

    for (int i = 1; i < line.Length; i++) { 
     v3 += force * (home - transform.position) * spacing; 
     t += spacing; 
     v3.y = y * t + 0.5f * Physics2D.gravity.y * t * t + transform.position.y; 
     line[i].transform.position = v3; 
      } 
} 

} 문제의

이미지 (풀 파워 샷) :

Full power shot. Weak prediction line.

당신은 같은 것을 할 수
+0

'힘'증가 또는 '중력 감소'가 도움이됩니까? – Xlander

+0

그게 도움이되는 동안, 나는 다소 어림 잡아서 시험해보고 오류를 일으키지 않고 제대로 조정할 수있는 오류가 있기를 바랬습니다. :) – Jed

+0

발사체의 'Vector2'가 출발점에서 멀어지는 동안'force'가 증가하고 있습니까? – Xlander

답변

0

:

private int actualFoce = 0; 

void Update(){ 
    Vector2 distanceFromCatapult = Vector2(transform.position.x - catapult.x, transform.position.y - catapult.y); 
    actualForce = distanceFromCatapult.magnitude + force; 

    //then the rest of your code 
} 

DisplayLine()에서 actualForce를 사용하고 발사체를 투사하십시오.

+0

고마워요. – Jed

+0

안녕하세요. :) – Xlander