Search Unity

AddForce to projectile in Camera Direction relative to rotation

Discussion in 'Scripting' started by James-Williams, Apr 4, 2018.

  1. James-Williams

    James-Williams

    Joined:
    Aug 30, 2016
    Posts:
    26
    Hey guys I am looking for a way to addForce in the direction the camera is facing. I am firing a projectile from an AR camera using Vuforia. Problem is that if i aim straight (0 degrees of rotation) ahead and fire, it fires and goes straight. If I tilt the camera down 5 degrees and fire, it fires as if i am still at 0 degrees of rotation.

    I am using a swipe mechanic to fire the projectile. Here is what I have so far.


    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.         if (canSwipe)
    4.         {
    5.             if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    6.             {
    7.                 final = Vector3.zero;
    8.                 length = 0;
    9.                 SW = false;
    10.                 Vector2 touchDeltaPosition = Input.GetTouch(0).position;
    11.                 startpos = new Vector3(touchDeltaPosition.x, 0, touchDeltaPosition.y);
    12.             }
    13.             if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
    14.             {
    15.                 SW = true;
    16.             }
    17.  
    18.             if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Canceled)
    19.             {
    20.                 SW = false;
    21.             }
    22.  
    23.             if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
    24.             {
    25.                 SW = false;
    26.             }
    27.             if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
    28.             {
    29.                 if (SW)
    30.                 {
    31.                     Vector2 touchPosition = Input.GetTouch(0).position;
    32.                     endpos = new Vector3(touchPosition.x, 0, touchPosition.y);
    33.                     final = endpos - startpos;
    34.                     length = final.magnitude;
    35.  
    36.                     Rigidbody clone = Instantiate(rbody, this.transform.position, this.transform.rotation);
    37.                     clone.transform.parent = parentObject.transform;
    38.  
    39.                     /*
    40.                      * not used did not work
    41.                     var camDir = Camera.main.transform.TransformDirection(Vector3.forward);
    42.                     camDir.z = 0.0f;
    43.                     */
    44.  
    45.                     clone.AddForce(new Vector3(0, touchPosition.y / 2, touchPosition.y));
    46.  
    47.  
    48.                 }
    49.             }
    50.         }
    51.     }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Code (csharp):
    1.  
    2. clone.AddForce(Camera.main.transform.forward * 20.0f); // change 20 to however strong you want
    3.  
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Yes, exactly what @GroZZleR said but it also looks like you have added an "up" term since I guess you want a parabolic arc that rises first.

    Modifying Grozzler's codelet, just add this term to what he passes to .AddForce():

    Code (csharp):
    1. + Camera.main.transform.up * 10.0f
    That should lift it similarly to your original force and send it upwards a bit first.
     
    GroZZleR likes this.
  4. James-Williams

    James-Williams

    Joined:
    Aug 30, 2016
    Posts:
    26
    thanks. I will try that. I got it to work by changing AddForce to RelativeForce
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Also, line 37 in your original codelet: not sure you want to be parenting your new object like that... if the parent object turns around, I'm not sure how Unity's physics solves that. Your object may turn with its parent. If you're just doing it for scene organization that's fine, but be aware there might be weirdnesses if you move the parent.
     
  6. James-Williams

    James-Williams

    Joined:
    Aug 30, 2016
    Posts:
    26