I want to use my raycast to guide my bullet direction. my player camera will point to any spot in the game and when he press “F” the tower gun will fire at that spot. But i can’t calculate the raycast hit point so then use it for the bullet !
Here is raycast code:
Camera mycamera;
public float bulletGoalx,bulletGoaly,bulletGoalz;
void Start() {
mycamera = GetComponent<Camera>();
}
void Update() {
Vector3 rayOrigin = new Vector3(0.5f, 0.5f, 0f); // center of the screen
float rayLength = 500f;
Ray ray = Camera.main.ViewportPointToRay(rayOrigin);
Debug.DrawRay(ray.origin, ray.direction * rayLength, Color.red);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, rayLength))
{
print ("I'm looking at : " + hit.transform.name);
// bulletGoalx =
// bulletGoaly =
// bulletGoalz =
}
}
Bullet code:
GameObject prefab;
public GameObject angle;
void Start () {
prefab = Resources.Load("TowerBullet") as GameObject;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
GameObject bullets = Instantiate(prefab) as GameObject;
bullets.transform.position = transform.position + angle.transform.forward * 0.1f;
Rigidbody rb = bullets.GetComponent<Rigidbody>();
rb.AddForce(angle.transform.forward * 50);
Destroy(bullets, 9f);
}
if (Input.GetMouseButtonDown(0))
{
Ray ray =Camera.main.ScreenPointToRay(Camera.main.transform.forward);
if (Physics.Raycast(ray, out hit, rayLength))
{
prefab = Resources.Load("TowerBullet") as GameObject;
GameObject bullet = Instanciate(prefab) as GameObject;
bullet.transfrom.position = [the position you want your bullet to start like the tower.transform];
bullet.GetComponent<BulletScript>().direction = hit.point;
}
}
public class BulletScript
{
public RigidBody rb;
GameObject prefab;
public GameObject angle;
public Vector3 direction;
void Start () {
rb = this.GetComponent<RigidBody>();
}
// Update is called once per frame
void Update () {
rb.MovePosition(direction * time.deltaTime);
}
}
hit.point is in world space so it is only the correct direction if you’re at the origin… if you want to use RaycastHit.point you’d need calculate the direction from something like “hit.point -transform.point” or, given that you’ve already calculated a ray you could just get it’s direction.
Thank you for you reply.
I tried to formate both scripts and i got one error in camera script and i could not fix it
This is camera script:
Camera mycamera;
public GameObject angle; // tower angle
public Rigidbody rb;
GameObject prefab;
public Vector3 direction;
void Start() {
mycamera = GetComponent<Camera>();
prefab = Resources.Load("bullets3") as GameObject;
}
void Update() {
Vector3 rayOrigin = new Vector3(0.5f, 0.5f, 0f); // center of the screen
float rayLength = 500f;
RaycastHit hit;
// Ray ray = Camera.main.ViewportPointToRay(rayOrigin);
// Debug.DrawRay(ray.origin, ray.direction * rayLength, Color.red);
//
// if (Physics.Raycast(ray, out hit, rayLength))
// {
// print ("I'm looking at : " + hit.transform.name);
// bulletGoalx =
// bulletGoaly =
// bulletGoalz =
// }
if (Input.GetMouseButtonDown(0))
{
Ray rays =Camera.main.ScreenPointToRay(Camera.main.transform.forward);
if (Physics.Raycast(rays, out hit, rayLength))
{
GameObject bullets3 = Instantiate (prefab) as GameObject;
bullets3.transfrom.position = transform.position + angle.transform.position; // it keep give me error here !!!!!!
bullets3.GetComponent<Bscript>().direction = hit.point;
}
}
// error code :: Error CS1061: 'UnityEngine.GameObject' does not contain a definition for 'transfrom' and no extension method 'transfrom' accepting a first argument of type 'UnityEngine.GameObject' could be found.
}