- Home /
Shoot arrow
Hello I try to make my player shoot arrows with my own character controller and the arrow is being instantiated in the right place but not moving afterwards here is the code
void Start()
{
Screen.lockCursor = true;
speed = 8;
}
// Update is called once per frame
void Update()
{
if(timer >= 1.5f || timer == 0)
{
timer = 0;
airTime = 0;
DrawArrow();
}
else
{
if(shot)
timer += Time.deltaTime;
}
}
void FixedUpdate () {
Debug.Log(timer + " " + shot);
moveDirection = (arrowPrefab.transform.rotation * Vector3.forward);
Debug.Log(moveDirection * speed);
moveDirection *= speed;
if(!arrowPrefab.GetComponent<CharacterController>().isGrounded){
arrowPrefab.GetComponent<CharacterController>().Move(moveDirection * speed);
arrowPrefab.transform.Rotate(0, Time.deltaTime * speed, 0);}
}
private void DrawArrow()
{
if(Input.GetButton("Fire1"))// starts drawing
{
shot = true;
Debug.Log("FIRE AT WILL");
Vector3 pos = new Vector3(transform.position.x, transform.position.y + 2, transform.position.z + 2);
Instantiate(arrowPrefab, pos, transform.rotation);
arrowPrefab.transform.rotation = Quaternion.Euler(30, 0, 0);
}
}
ty very much
Answer by flaviusxvii · Dec 13, 2013 at 10:17 PM
I have a few suggestions. The component that fires and arrow shouldn't be the same component that controls the flight/path of the arrow.
You are referring to "arrowPrefab" in a few places, and it doesn't make sense. If you make an instance of arrowPrefab with Instantiate, that returns a reference to a NEW arrow. So that new arrow should have the flight/path component on it.
You should do some tutorials. http://www.youtube.com/user/infiniteammoinc
thx for the answer im new to physics in unity used to plain microsoft c#
Answer by $$anonymous$$ · Apr 05, 2016 at 05:35 AM
https://www.assetstore.unity3d.com/en/#!/content/58751
Check this asset, it has full support for shooting
Your answer