- Home /
Pointing 2 empty game objects towards one point? (Raycast?)
So I'm making a special little fps game where you have to guns in each hand! Now the problem is that I cant hit with both of these guns shooting at the same time since the arms isn't IN the players body!
Could I possibly use something like raycast that centers the screen and then make the empty game objects rotate in that directions so the guns shoot at the same location? Maybe even have a little bit of a spread so they dont go exactly at the same place?
Please describe a lot since I never have used raycast or something simular before!
Thanks!
//Elis
Answer by robertbu · Mar 03, 2013 at 10:18 AM
Here is a simple example. Attach this script to an empty game object just in front of the gun. Drag your bullet prefab onto the goPrefab variable in the inspector. Space bar fires it.
using UnityEngine;
using System.Collections;
public class CameraCenterRaycast : MonoBehaviour {
public GameObject goPrefab;
public float defaultDist = 10;
void Update () {
if (Input.GetKeyDown (KeyCode.Space))
{
Vector3 v3Pos = Vector3.zero;
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit)) {
v3Pos = hit.point;
}
else
v3Pos = Camera.main.transform.position + Camera.main.transform.forward * defaultDist;
// Play with your point here to make each gun fire a bit
differently
transform.LookAt(v3Pos);
GameObject go = (GameObject)Instantiate (goPrefab, transform.position, transform.rotation);
go.rigidbody.AddRelativeForce (Vector3.forward * 1000.0f);
}
}
}
Your answer
Follow this Question
Related Questions
how would i add bullet spread? 0 Answers
Vertical auto-aim with raycasts 1 Answer
Networked 3rd person aiming 0 Answers
Raycast not rotating with object it casts from 0 Answers
Aiming a shot in Unity 2d? 0 Answers