D
D
Deathgar2017-03-07 19:32:59
C++ / C#
Deathgar, 2017-03-07 19:32:59

Archery, how to implement the release of the arrow?

There is a code:

using UnityEngine;
using System.Collections;

public class Bow : MonoBehaviour {
  public Transform arrowPrefab; //префаб стрелы
  private bool checkArrow; //проверка создания стрелы(создана или нет)
  private Transform arrow; //созданная стрела в окне
  public float speedArrow; // скорость стрелы
  private Transform transformSpawners;//расположение лука
  // Use this for initialization
  void Start () {
    checkArrow = false;
    transformSpawners = gameObject.transform;
  }
  
  // Update is called once per frame
  void Update () {

    if (Input.GetMouseButtonDown(0) && checkArrow == false) {
      arrow = (Transform)Instantiate(arrowPrefab, 
                          transformSpawners.position,
                          Quaternion.identity);
      arrow.transform.rotation = transformSpawners.rotation;

      checkArrow = true;

    }		
    if (checkArrow && Input.GetMouseButton(0)) {
      arrow.transform.position = GameObject.Find ("Spawn").transform.position;
    }
    if(checkArrow && Input.GetMouseButtonUp(1))
    {
      Destroy(arrow.gameObject);
      checkArrow = false;
    }

    if(checkArrow && Input.GetMouseButtonUp(0))
    {	
      checkArrow = false;
    }
  }
}

How to implement archery at this moment, and even on a parabola?
I tried to change AddForce in different ways, but it didn’t work, I just tried to change the transform, it didn’t work either.
if(checkArrow && Input.GetMouseButtonUp(0))
    {	
      checkArrow = false;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2017-03-07
@Deathgar

arrowPrefab do you have an object with a Rigidbody ?
AddForce works great if there is a physical body. Just do not forget that the type of force is different, for the case of an arrow, momentum is great.
something like this
arrow.AddForce(arrow.transform.forward * forcePower, ForceMode.Impulse);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question