AddForce(C#) not works

tenho um prefab que estou tentando instanciá-lo em um script e aplicar a ele uma força inicial de lançamento, consigo instanciar mas não consigo aplicar esta força, meu código:

public class MissileLauncherC : MonoBehaviour 
{
    public  Rigidbody Projectile;
    public float speed = 20;
    void Start()
    void  Update ()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Rigidbody teste;
            teste = Instantiate(Projectile, transform.position, transform.rotation) as Rigidbody;
            teste.rigidbody.AddForce(transform.forward * speed * Time.deltaTime, ForceMode.Impulse);			
            Physics.IgnoreCollision(Projectile.collider, collider);
        }
    }
}

alguém pode me ajudar?

I have a prefab that I’m trying to instantiate it in a script and apply a force to it initial launch, I can instantiate but can not apply this force.

can anyone help me?

Time.deltaTime is only needed when you apply a continuous force every frame. An impuls is a one time thing, so remove the Time.deltaTime. Also keep in mind that ForceMode.Impulse takes the mass of the object into account. The greater the mass the smaller the effect. You might want to use ForceMode.VelocityChange instead or check your speed / mass ratio.

i still can not, my script:
clone.AddForce = Vector3.forward * Speed;

the object is instantiated but not hurled
i also tried:
clone.velocity = Vector3.forward * Speed;
clone.rigidbody.AddForce(clone.transform.forward * speed);
and others bud did not work :frowning:

consegui agora :slight_smile:

o código completo:

using UnityEngine;
using System.Collections;

public class MissileLauncherC : MonoBehaviour

{
public Rigidbody Projectile;

public Transform Launcher;

public float Speed = 20;

void Start()
{
	
}

void Update ()
{

if (Input.GetButtonDown("Fire1"))
	{
		Rigidbody clone;
		clone = Instantiate(Projectile, Launcher.position, Launcher.rotation) as Rigidbody;
		clone.velocity = transform.forward * Speed;	
		
	}
}

}

sou novo por aqui mas estarei a disposição para ajudar sempre quando puder!

First thing to check if AddForce doesn’t work is to make sure your object as a RigidBody and does not have ‘isKinematic’ turned on

If those things are good then another common mistake is to try to addforce to a GameObject you just initiated. If that is the case then don’t forget to set your initiated object to a gameobject that you can then alter (don’t miss the casting ‘as GameObject’!!!)

public GameObject PREFAB;
public Vector3 THE_OBJECT_SPAWN_TRANSFORM;

// Use this for initialization
void Start () {
	StartCoroutine (CreateNewObjectToMove());
}

IEnumerator CreateNewObjectToMove()
{
    yield return new WaitForSeconds (2);

	Quaternion OBJECT_ROTATION = Quaternion.identity;
	GameObject OBJECT_I_CAN_CHANGE;

	OBJECT_I_CAN_CHANGE = Instantiate (PREFAB, THE_OBJECT_SPAWN_TRANSFORM, OBJECT_ROTATION) as GameObject;
	OBJECT_I_CAN_CHANGE.rigidbody.AddForce(PREFAB.transform.forward * 200);
	
}

public class PlayerControls : MonoBehaviour {
public GameObject ball;
public int numbBallAllowed;
public Transform servePoint;
private GameObject numbOfBalls;
public float ballForce = 1000f;
void Start ()
{

}

void Update ()
{
    numbOfBalls = GameObject.FindGameObjectsWithTag("Ball");
    if (Input.GetKeyDown(KeyCode.R) && numbOfBalls.Length +1 <= numbBallAllowed)
    {
        GameObject balls;
        balls = Instantiate(ball, servePoint.position, servePoint.rotation) as GameObject;
        balls.rigidbody.AddForce(ball.transform.forward * ballForce);
    }


}

}
// doesnt work, anyone know why?