I'm developing a Unity game using C#. I need to throw an object from the main camera. Trying to do it with Javascript seems to work, but I need to do it with C#, and simulating the same script behavior it does not work. These are the codes I'm using for it:
Javascript (does work)
var vel : int = 2;
var objeto : Transform;
function Start () {
}
function Update () {
if (Input.GetKeyDown("mouse 1")){
var generar = Instantiate (objeto, transform.position, Quaternion.identity);
generar.GetComponent.<Rigidbody>().AddForce(transform.forward * 2000 * vel);
}
}
C# (it creates de object, but don't apply the force)
using UnityEngine;
using System.Collections;
public class LanzarCX : MonoBehaviour {
public Transform objeto;
public int vel= 2;
void Start () {
}
void Update () {
if (Input.GetKeyDown("mouse 1")){
GameObject generar = Instantiate (objeto, transform.position, Quaternion.identity) as GameObject;
generar.GetComponent<Rigidbody>().AddForce(transform.forward * 2000 * vel);
}
}
}
I'm using 2 as vel value and the same prefab in both cases. What do I'm doing wrong? Thank you
Did you check that your RigidBody is active? According to Unity API: "Force can be applied only to an active rigidbody. If a GameObject is inactive, AddForce has no effect." Also, in your javascript code you aren't multiplying by vel