- Home /
[SOLVED] Shooting from Elemental Staff
Okay, sorry to bug you guys again. If I was to create an elemental staff within a model (it has no firing animation etc) what do I need to do in order to shoot from the staff in the straight line it's been clicked on?
I've attached a Sphere Gameobject as a child of the staff, but I have no idea on what I need to do in order for when I click my mouse that it shoots that sphere.
If you guys have any tutorials, please do tell as I'm actually wanting to learn how Unity works!
Thanks
Answer by rutter · Apr 08, 2012 at 03:08 AM
You could use Vector3.Lerp() to move the sphere from initial position to target position, over time.
The Unity manual page on prefab instantiation mentions a few methods for firing projectiles. You could look into adapting those to suit your purposes. When example scripts reference a "direction" for the shot, you can provide a direction by subtracting the target position from the shot origin -- this will give you a vector pointing from one position to the other.
Okay, I used that to generate my code, but at the moment it just generates a stationary sphere. Although after trying to uhh.. Implement Vector3.Lerp, I'm kind of flabbergasted.
The code which I came up with is this:
var bullet : Transform;
var target : Transform;
function Update () {
if ($$anonymous$$otor.toggleCombat$$anonymous$$ode){
if(Input.Get$$anonymous$$ouseButton(0)){
var hit: RaycastHit;
if (Physics.Raycast(target.position,target.forward, hit)) {
var hitpoint = hit.point;
transform.LookAt(hitpoint);
var bullet1 : GameObject = Instantiate (bullet , GameObject.Find("spawnPoint").transform.position, UnityEngine.Quaternion.identity);
bullet1.transform.rotation = transform.rotation;
bullet1.rigidbody.AddForce(transform.forward * 1000);
}
}
}
}
This is after I gave up with Vector3.Lerp. The code ^ just creates a stationary prefab at the 'spawnPoint' of the elemental staff. Any suggestions? :/
Your answer
Follow this Question
Related Questions
Can someone help me with my script? 0 Answers
Enemy not getting hit (Collider issue) 2 Answers
how to take maya animation file in to unity? 1 Answer
Unity model scripting question 1 Answer