Here’s a video of my very flawed mechanics, I’ve posted in the forum but I’m gonna ask here about a specific issue that occurs at 2:01 - YouTube
Here is the script attached to camera which is a child object mouseorbiting around a player mesh the spawnPoint of the bullet prefabs is a child of the camera so the bullets shoot out from the camera lol. What i moved the spawn point to the mesh’s hand or something it was aiming totally off and offset:
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour {
public AudioClip shootSound;
public float shootForce = 2500;
public GameObject spawnPoint;
public GameObject bulletPF;
public GameObject Character;
private GameObject projectile;
public bool isLaserState = false;
void Update() {
if(isLaserState){
if(Input.GetMouseButton(0)) {
audio.PlayOneShot(shootSound);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);//hit exits now
projectile = Instantiate(
bulletPF,
spawnPoint.transform.position,
spawnPoint.transform.rotation) as GameObject;
Debug.DrawLine(spawnPoint.transform.position, hit.point);
projectile.transform.LookAt(hit.point);
Physics.IgnoreCollision(projectile.collider, Character.collider);
projectile.rigidbody.AddForceAtPosition(ray.direction * shootForce, hit.point);
}
}else if (Input.GetMouseButtonDown(0)) {
audio.PlayOneShot(shootSound);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);//hit exits now
projectile = Instantiate(
bulletPF,
spawnPoint.transform.position,
spawnPoint.transform.rotation) as GameObject;
Debug.DrawLine(spawnPoint.transform.position, hit.point);
projectile.transform.LookAt(hit.point);
Physics.IgnoreCollision(projectile.collider, Character.collider);
projectile.rigidbody.AddForceAtPosition(ray.direction * shootForce, hit.point);
}
}//end of Update
}//end of class
So I actually just got it to work with some help from #unity3D on IRC.
credit definitely goes to user Dezoaan on #unity3D
this code fixes it:
It has everything to do with the new variable added in called ‘aimPoint’ which is a vector3 that if hit.point = (0.0,0.0,0.0) then aimPoint = to a point 100 units into the ray. so now you have a point to lookAt no matter what!
Here’s a youtube video of it working. notice how spawnPoint is placed in front of the model now. resembling some sort of gun shooter or bullet emitting area.
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour {
public AudioClip shootSound;
public float shootForce = 2500;
public GameObject spawnPoint;
public GameObject bulletPF;
public GameObject Character;
private GameObject projectile;
public bool isLaserState = false;
int ammo = 20;
void Update() {
if(isLaserState){
if(Input.GetMouseButton(0)) {
audio.PlayOneShot(shootSound);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//straight ray to mouse position
Vector3 aimPoint;//hit point placeholder
if (Physics.Raycast(ray, out hit, 100)){//where the ray hits, so draw from anywhere to THIS = raycast from new THAT
aimPoint = hit.point;
} else { //if ray doesn't hit anything, just make a point 100 units out into ray, to referece
aimPoint = ray.origin + (ray.direction * 100);//aimPoint is some point 100 unitys into ray line
}
projectile = Instantiate( bulletPF, spawnPoint.transform.position, spawnPoint.transform.rotation) as GameObject;
projectile.transform.LookAt(aimPoint); //fixes when hit point was = (0,0,0);
Debug.DrawLine(spawnPoint.transform.position, aimPoint);
Physics.IgnoreCollision(projectile.collider, Character.collider);
projectile.rigidbody.velocity = projectile.transform.forward * 80;//this plus the LookAt aimpoint sends a bullet on the correct ray
ammo = 20;
}
// END OF LASERSTATE SHOOTING
}else if (Input.GetMouseButtonDown(0) && (ammo > 0)) {//normal shooting, since button down
audio.PlayOneShot(shootSound);
ammo--;
RaycastHit hit; //hit exists now
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 aimPoint;
if (Physics.Raycast(ray, out hit, 100)){//same amazing check, credit to Deozaan #Unity3D on IRC
aimPoint = hit.point;
} else {
aimPoint = ray.origin + (ray.direction * 100);
}
projectile = (GameObject)Instantiate( bulletPF, spawnPoint.transform.position, spawnPoint.transform.rotation) as GameObject;
Debug.DrawLine(spawnPoint.transform.position, aimPoint);
Debug.Log("this is hit point: " + hit.point);
//so it is 0,0,0 when i hit nothing
Debug.Log("this is null hit: " + aimPoint);
projectile.transform.LookAt(aimPoint);//send it on the ray
Physics.IgnoreCollision(projectile.collider, Character.collider);// don't hit character
projectile.rigidbody.velocity = projectile.transform.forward * 80;
}
}//end of Update
}//end of class