I’m working on a simple melee script. The old one was a simple rocket that was quickly destroyed after launch with the playable character having a swing animation.
Now I figured that if the bullet simply stayed on the sword then it would look more like a real sword swing.
The object does appear and is a rigidbody.
Here’s the old script. Any idea how to get the instantiated rigidbody to constantly update it’s position to that of where it was instantiated from?
It allready does it to determine where it should be instantiated in the first place but it has to do that the whole time while it still is alive.
var projectile : Rigidbody;
var initialSpeed = 20.0;
var reloadTime = 0.5;
//var shootGUI : GUIText;
//var Shots = 0.0;
private var lastShot = -10.0;
function Update () {
//if (Input.GetButton ("Fire1"))
//Fire();
}
function Fire () {
if (Time.time > reloadTime + lastShot ) {
// create a new projectile, use the same position and rotation as the Launcher.
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
// Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
//Shots ++;
//UpdateGUI(Shots);
}
}
//function UpdateGUI (points) {
//shootGUI.text = points.ToString();
//scoreGUI.text = points.ToString();
//}
I’ve allready tried setting the velocity to 0 but then it just hangs in the air.
Without that whole line it does the same.
When something doesn’t work, you need to explain how it doesn’t work or it’s harder to fix and many people won’t bother.
For instance, you would say “it’s not working because there’s an error” and then post the error.
When you create the bullet, set its parent to the sword. You don’t have to update that every update.
bullet.transform.parent = sword.transform;
Like KyleStaves said.
(gameObject.name == “Turret”) is false unless the gameObject the script is attached to is named Turret. So you’re setting theParent = false. Since you’re not explicitly typing it (“var theParent : Transform = blah”), Unity won’t tell you you’re doing something silly and will just let you set your variable to whatever. It’s always best to use explicit types when possible.
I’m currently trying this stuff out in a placeholder setting. So you have a boat that has a turret and the turret has MouseLook. The turret has this script to fire a “RigidBody”:
var projectile : Rigidbody;
var initialSpeed = 20.0;
var reloadTime = 0.5;
private var lastShot = -10.0;
function Fire () {
if (Time.time > reloadTime + lastShot ) {
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
}
}
It’s basically the rocket launcher code.
Now my Rigidbody that is being instantiated has this script:
// The reference to the explosion prefab
var timeOut = 3.0;
// Kill the rocket after a while automatically
function Start () {
Invoke("Kill", timeOut);
}
function OnCollisionEnter (collision : Collision) {
// Instantiate explosion at the impact point and rotate the explosion
// so that the y-axis faces along the surface normal
var contact : ContactPoint = collision.contacts[0];
var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
// And kill our selves
Kill ();
}
function Kill () {
// Destroy the projectile
Destroy(gameObject);
}
The name of the gameobject from where the projectile is instantiated is called Turret.
So I need a few lines of code that says that the instantiated rigidbody will stay on the Turret.
I don’t know how to call the Turret in the projectile script. Any ideas?