- Home /
Question by
Bill Got Game · Nov 23, 2013 at 03:47 PM ·
instantiateprefabfps
instansiating prefab, then adding force in camera direction C#
hey guys . would appriaiate any hand with this code. cant seem to get it to work. the gui at the bottom is just a force indicator. i want to be able to throw a ball in the direction the fps camera is looking
public class FireProjectile : MonoBehaviour {
public GameObject projectile;
public float force = 0;
// Update is called once per frame
void Update ()
{
if (Input.GetButton("Fire1"))
{
force += 20 * Time.deltaTime;
}
if (Input.GetButtonUp("Fire1"))
{
projectile.rigidbody.AddForce(Camera.main.transform.right * force);
GameObject pro = Instantiate(projectile,transform.position, Quaternion.identity) as GameObject;
force = 0;
}
}
void OnGUI()
{
string forceText = GUILayout.TextField(force.ToString("0.00"));
float newForce;
if (float.TryParse(forceText, out newForce))
{
force = newForce;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613122555im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Updating a variable on a script in an instanced object 1 Answer
Instantiate object 1 Answer
Prefab instantiated wrong scale on mobile 0 Answers
autolink to prefab? 0 Answers