So what my goal is, is to have a script that when attached to a player or camera or whatever,
it shoots my bullet prefab on left mouse click in the direction of the crosshair/mouse on my screen.
Now I don’t know if I worded that wrong or if that idea is wrong. Maybe that’s not what I want, an example I can give is like the shooting in mechanic in games like Zone of the Enders, Armored Core (3) or any third person robot third person shooting game.
Basically to have the crosshair on screen in 3rd person. and shoot to the crosshair from the robot mesh that the camera rotates around,
and not shoot from the camera like the way I have it now.
I made the character mesh not collide with my bullets to compensate
and having the bullets right on the camera’s face is no bueno…
So yeah how do I fix this so that it shoots correctly from the player mesh, I guess for now the spawn point can be like a sphere at the center of the mesh then moved forward a bit. So like a chest energy blaster that can shoot in all directions accurately. Also i have some scripts on the camera which may affecting this, like mouse orbit.cs and stuff.
This one is possibly easier to solve, please help me with one or the other, i really appreciate it!
Also problem #2 is that if the ray cast hits, it does the the lookAt function correctly and the bullet’s position is normal and good. otherwise if you shoot off into space it just goes off like a wobbling rigidbody…
The debug shows the line every time i click and the bullet follows the path every time but only with hit does it do the lookAt correctly when it fires.
You can really see this problem at 1:04 in the above video. like it’s clear!
Also just noticing that I travel as fast as my bullets… and i know making rigidbody addforce too high on my bullet prefabs will mess with collision. So I should probably use ray casts for firing bullets huh… I kind of like the strafe effect. Although I know everything I’ve done here is NOT optimal or good code, also i’m a total beginner so it’s to be expected I scrap everything now and then, this is just a learning project not a game.
Also just noticed… i had to drag the camera with my mouse… like drag off to the right keep on going to turn lol…
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
Also the laserState is true when I pick up the little spinning metroid looking thing