So, I’m trying to do a simple shooting thing. All I need it to do is send a sphere forward from the camera at left click. I have the bullet and the target both set as rigid bodies with gravity and angular drag off. Here’s the script for the bullet:
var isActive:boolean = false;
var playerT:Transform;
function Update () {
getInput();
if(isActive) {
transform.position += transform.forward * 10;
}
}
function getInput() {
if(Input.GetButton("Fire1")) {
transform.forward = playerT.forward;
transform.position = playerT.position;
isActive = true;
}
}
function OnCollisionEnter(other : Collision) {
print("This collider is named: " + other.contacts[0].otherCollider.name);
}
playerT is the transform from the MainCamera. The problem is that after the first collision if I fire the bullet again its forward vector starts to change mid-flight. It starts correct, but then it will start curving. Sometimes it’s so extreme the bullet corkscrews down the range. I’m thinking there is something about rigid bodies that I don’t know about, because it didn’t do this before I made the bullet a rigid body. Thanks.
I think that you have to apply a forward force instead of translate. Try something like this:
var explosionPower: float = 2000.0;
//bullet.rigidbody.AddForce(transform.forward * explosionPower);
rigidbody.AddForce(transform.forward * explosionPower);
instead of
transform.position += transform.forward * 10;
That didn’t do it. But I fixed it another way. I just reset the location of the bullet to the camera’s location and set isActive to false on collision. Not exactly sure what happened, it’s kind of a dead rat, but whatever. Thanks anyways.
Ok, more issues I could use some help with.
Script is attached to bullet object. I have a field variable called muzzleVel, it’s a float with value 2820.
I move the bullet via this line:
transform.position -= transform.forward * (muzzleVel * Time.deltaTime);
The issue I’m having is that changing the value of muzzleVel doesn’t do anything. I can’t imagine why this would be, there must be something I’m missing. I’ll post the whole script below for reference. Thanks.
var timeDilation:float = 0.33;
var isActive:boolean = false;
var playerT:Transform;
var hitMarkerObj:GameObject;
var bulletCam:GameObject;
var mainCam:GameObject;
var muzzleVel:float = 2820; //fps
var flightDistance:float = 0.0;
var RPS:float = 0.0;
var twist:float = 10.0;
function Start() {
bulletCam.active = false;
}
function Update () {
getInput();
if(isActive) {
transform.position -= transform.forward * (muzzleVel * Time.deltaTime);
flightDistance += (muzzleVel * Time.deltaTime);
transform.Rotate(0, 0, (360*(RPS*Time.deltaTime)));
bulletCam.active = true;
mainCam.active = false;
bulletCam.transform.position = transform.position - Vector3(1, 0, 1);
bulletCam.transform.rotation.y = 0;
bulletCam.transform.Rotate(0, 45, 0);
if(flightDistance > 3500) {
isActive = false;
transform.position = playerT.position;
}
} else {
bulletCam.active = false;
mainCam.active = true;
}
}
function getInput() {
if(Input.GetButton("Fire1")) {
transform.forward = -playerT.forward;
transform.position = playerT.position;
isActive = true;
flightDistance = 0;
RPS = (12/twist)*muzzleVel;
}
}
function OnCollisionEnter(other : Collision) {
print("This collider is named: " + other.contacts[0].otherCollider.name);
var hitMarkerGO:GameObject = Instantiate(hitMarkerObj, other.contacts[0].point, Quaternion.AngleAxis(180, Vector3.up));
hitMarkerGO.transform.position.z -= 20;
transform.position = playerT.position;
isActive = false;
}