- Home /
Having some issues with firing a "projectile"
Here is the projectile script I am attempting to use. Please bare with me I am extremely new to programming and Unity.
var projectile : Rigidbody;
var damage = 5;
function update () {
if(Input.GetButtonDown("Fire1")){
Spawnpoint = transform.Find("/First Person Controller/Main Camera/Spawnpoint");
SpawnVector = Vector3(Spawnpoint.position.x, Spawnpoint.position.y, Spawnpoint.position.z);
var clone: Rigidbody;
clone = Instantiate(projectile, SpawnVector, Spawnpoint.rotation);
clone.velocity = Spawnpoint.TransformDirection (SpawnVector.forward*20);
}
}
There are a few issues I'm having. But the main issue I'm having is that my projectile does not fire at all when I 'left click'. Is there somewhere else I need to define "Fire1" - I was under the impression that it was built into the First Person Controller.
Any insight into my problem would be much appreciated. As I said. I am very new and I attempted to look up a solution to this issue but only found other working and much more complicated scripts. I'd really prefer to learn on my own and I hope I didn't inconvenience anyone by posting here.
Thank you in advance for your time!
You only needs to put the script in one active game object. If I didn't remember wrong, Fire1 is set by default to left click. Try to put 'Debug.Log("Activates");' after the 'if' to know if the input is working.
Answer by Landern · Jan 10, 2013 at 01:38 PM
If your projectile is/contains a rigidbody, as the documentation says for Rigidbody.velocity:
In most cases you should not modify the velocity directly, as this can result in unrealistic behavior. Don't set the velocity of an object every physics step, this will lead to unrealistic physics simulation.
My suggestion is, take a look at your Input Manager
Next, you should move any rigidbody code to FixedUpdate instead of the Update method.
Use AddForce to get it moving forward, please keep in mind gravity may pull your bullet down, disable gravity on the rigidbody for the bullet.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I make a respawn system? 1 Answer
Why does my var speed say it is wrong 2 Answers
How do you code in unity 3.5 and what if any programs would you need 2 Answers
Rest postion after animation plays 0 Answers