- Home /
Add force to GameObject
Hallo! Ich habe in mein Spiel eine Funktion eingebaut mit welcher man schießen kann, leider hat keine der Methoden die ich probiert habe funktioniert dem Projektil eine Geschwindigkeit zu geben, demnach spawnt es und fällt gerade runter. Hilfe pls!
Hello! I included in my game a function with which you can shoot, unfortunately, none of the methods I've tried works to give the projectile a speed, therefore it spawns and falls straight down. Help pls!
The Code:
class PersonScriptShooting {
var projectile : GameObject;
var spawnpoint : Transform;
public var damage = 5;
}
var launchProjectile : PersonScriptShooting = PersonScriptShooting();
function shoot()
{
if(Input.GetButtonDown("Fire1"))
{
var Vektor : Vector3 = Vector3(launchProjectile.spawnpoint.position.x,launchProjectile.spawnpoint.position.y,launchProjectile.spawnpoint.position.z);
GameObject.Instantiate(launchProjectile.projectile,Vektor,Quaternion.identity);
// var elevation : Vector3 = Quaternion.Euler(Vektor) * launchProjectile.Spawnpoint.forward;
// launchProjectile.projectile.rigidbody.AddForce(elevation * 10);
launchProjectile.Projectile.velocity = transform.TransformDirection (Vector3.forward * 10);
}
}
Answer by Skullwing · Feb 14, 2013 at 02:15 AM
function shoot()
{
if(Input.GetButtonDown("Fire1"))
{
var spawn : Vector3 = Vector3(launchProjectile.spawnpoint.position.x,launchProjectile.spawnpoint.position.y,launchProjectile.spawnpoint.position.z);
var clone : GameObject = Instantiate(launchProjectile.projectile,spawn,launchProjectile.camera.rotation);
clone.rigidbody.AddForce((spawn - Vector3(launchProjectile.camera.position.x,launchProjectile.camera.position.y,launchProjectile.camera.position.z)) * 50, ForceMode.Impulse);
}
}
Answer by RolandasR · Feb 10, 2013 at 08:12 PM
you need to have Rigidbody on your GameObject for forces to work
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
What variables can i declare? 1 Answer
Destroy GameObject A or B 1 Answer
How do I add force in the on trigger 1 Answer
Audio not playing [Fixed] 1 Answer