- Home /
Im trying to get an arrow to shoot with mouse click but the arrow doesn't show up even though on the hierarchy menu it's showing up.
Hi,
Im trying to get an arrow to shoot with mouse click but the arrow doesn't show up even though on the hierarchy menu it's showing up as projectile clone.
Please help #dying using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ProjectileShooter : MonoBehaviour {
GameObject prefab;
void Start () {
//calling out the arrow prefab
prefab = Resources.Load("projectile") as GameObject;
}
void Update () {
//if player left clicks mouse it shoots
if (Input.GetMouseButtonDown(0))
{
//new variable for the projectile | Instantiate: to create an instance of an object
GameObject projectile = Instantiate(prefab) as GameObject;
//projectile is from both the players and camera's positions + its *2 forward so its not inside characters head
projectile.transform.position = transform.position + Camera.main.transform.forward * 2;
//make it move at the right speed
Rigidbody rb = projectile.GetComponent<Rigidbody>();
//take where were looking + speed
rb.velocity = Camera.main.transform.forward * 40;
}
}
}
Hi,
Its showing up now that its bigger buuuuuuut it appears and disappears it doesn't actually go forwards and its really fast.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Arrow : $$anonymous$$onoBehaviour {
private GameObject arrowPrefab;
// Use this for initialization
private void Start () {
arrowPrefab = Resources.Load("projectile") as GameObject;
}
// Update is called once per frame
void Update () {
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
GameObject newArrow = Instantiate(arrowPrefab) as GameObject;
newArrow.transform.position = transform.position;
Rigidbody rb2 = newArrow.GetComponent<Rigidbody>();
rb2.velocity = Camera.main.transform.forward * 30;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArrowPt2 : $$anonymous$$onoBehaviour {
Rigidbody rb2;
// Use this for initialization
void Start () {
rb2 = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
transform.rotation = Quaternion.LookRotation(rb2.velocity);
}
}
well thats because 30 velocity is so much, lowering the speed didnt help?
You can pause the game right after you shoot the projectile, go to scene view, select the projectile in the hierarchy and press F to zoom in on it to see where it is etc.
Your answer
Follow this Question
Related Questions
Change a prefab sprite using Random.range instantiate in a script 3 Answers
How to instantiate a projectile only from the weapon prefab of the firing player? 1 Answer
Don't render prefab until onCollision - related, parent prefab.. 0 Answers
Instantiated Object being auto-deleted 0 Answers
Issue Instantiating prefab in C# 0 Answers