Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instantiate and apply force to object in camera direction

Discussion in 'Scripting' started by lixyna, Jul 6, 2017.

  1. lixyna

    lixyna

    Joined:
    Jun 26, 2017
    Posts:
    1
    Im trying to instantiate a prefab object just in front of my first person character (with the standard fps char script) and then push it into the direction im looking at with adjustable force. However, everytime I try to do so, the object always gets instantiated not in front of the camera, but in front of the character itself at the same Y value, and the force is not applied in the Y direction so the object just falls flat on its face after some distance. Since my project is about throwing things and creating a trackable projectile movement parabola, being able to throw things upwards kind of is a mandatory thing.
    I assume I dont apply the force correctly or use the wrong Vector, but I have no idea how to fix it.

    This is my whole script (ignore some Debug things, I was desperate)

    Code (CSharp):
    1. public class throwScript : MonoBehaviour {
    2.  
    3.     public float force = 1F;
    4.  
    5.     public GameObject Throwable;
    6.     public GameObject Thrower;
    7.     private Transform start;
    8.     private Boolean flag = false; //unused
    9.     public float MaxForce = 1000.0F;
    10.     private float tempforce;
    11.     private int delay = 10;
    12.  
    13.  
    14.     public GameObject InstantiateObject()
    15.     {
    16.         start = Thrower.GetComponent<Transform>();
    17.         var temp = Instantiate(Throwable, start.position+transform.forward*2, start.rotation);
    18.  
    19.         Debug.Log("Force after Instantiate = " + tempforce);
    20.         Debug.Log(temp.transform.position);
    21.  
    22.         return temp;
    23.     }
    24.  
    25.     // Use this for initialization
    26.     void Start () {
    27.          start = Thrower.GetComponent<Transform>();
    28.          tempforce = force;
    29.     }
    30.    
    31.     // Update is called once per frame
    32.     void Update () {
    33.    
    34.     }
    35.     void FixedUpdate()
    36.     {
    37.         delay++;
    38.        
    39.         if (Input.GetKey(KeyCode.Mouse0) && delay >10)
    40.         {
    41.             tempforce = tempforce + 10.5F;
    42.             if (tempforce > MaxForce) tempforce = MaxForce;
    43.          
    44.         }
    45.  
    46.         if(Input.GetKeyDown(KeyCode.Mouse1)) { start = Thrower.GetComponent<Transform>(); Debug.Log(start.position+transform.forward*5); }
    47.  
    48.  
    49.     }
    50.     private void LateUpdate()
    51.     {
    52.         if (Input.GetKeyUp(KeyCode.Mouse0) && delay > 10)
    53.         {
    54.             GameObject temp = InstantiateObject();
    55.             temp.GetComponent<Rigidbody>().AddRelativeForce(0, 0, tempforce);
    56.             tempforce = force;
    57.             delay = 0;
    58.         }
    59.     }
    60. }
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    AddRelativeForce adds a force relative to the rigidbody's coordinate space, not the caller's coordinate space. Your Throwable's rigidbody is getting a force applied in its own z direction, which may not be the same as the camera's z direction. Instead, you should calculate the camera's force in world space, then apply that force to your throwable with regular rigidbody.AddForce().