- Home /
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?
Answer by Bunny83 · Oct 29, 2012 at 10:56 PM
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.
btw, it looks like your Start method is missing the body... $$anonymous$$aybe just a copy & paste error or it got lost during the formatting here.
Answer by godoy · Oct 31, 2012 at 12:08 PM
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 :(
Answer by godoy · Nov 01, 2012 at 05:52 AM
consegui agora :)
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!
Answer by CollosalChris · Mar 20, 2014 at 09:46 PM
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);
}
Answer by etcarew · Mar 03, 2016 at 03:14 PM
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?
Your answer
Follow this Question
Related Questions
Apply force then slowly back off 4 Answers
Why can't Animation run functions with Rigidbody.AddForce? 1 Answer
Bullet add force to ragdoll help 0 Answers