Hi all,
I’m successfully instantiating my projectile, but not able to affect its rigibody with GetComponent.
Any ideas?
// Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
// Do test the code! You usually need to change a few small bits.
using UnityEngine;
using System.Collections;
public class ShooterforC : MonoBehaviour {
public Transform character;
public GameObject projectile;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
public Camera main;
private Transform clone;
RaycastHit hit;
void Update()
{
if (Input.GetMouseButton(0) && Time.time > nextFire) {
//if (Input.GetButton("Fire1") && Time.time > nextFire)
{
RaycastHit hit;
float distance = 100;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(ray,out hit)){
Debug.DrawLine (character.position, hit.point);
nextFire = Time.time + fireRate;
GameObject clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
nextFire = Time.time + fireRate;
projectile.transform.LookAt(hit.point);
projectile.GetComponent<Rigidbody>().velocity = projectile.transform.forward * 15;
}
}
}
}}