Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

mtion projectile with rigidbody

Discussion in 'Physics' started by chucky831, Aug 19, 2019.

  1. chucky831

    chucky831

    Joined:
    Aug 24, 2012
    Posts:
    265
    Hi everyone, I'm trying to realize the parabolic motion of the projectile using the transforms and it works. The problem is when I use rigidbody.velocity that doesn't work well. Can you give me directions? thank you
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class proiettile : MonoBehaviour {
    7.  
    8.     // Use this for initialization
    9.     public Vector2 initialVelocity;
    10.     public Vector2 startPosition;
    11.     private Rigidbody rgb;
    12.     public float angle;
    13.     private float radians;
    14.     private float gravity;
    15.     private float x;
    16.     private float y,t;
    17.     public bool flag;
    18.    void Start () {
    19.        
    20.         flag = false;
    21.         t = 0;
    22.         rgb = GetComponent<Rigidbody>();
    23.        // this.transform.position = startPosition;
    24.    }
    25.    
    26.    // Update is called once per frame
    27.    void FixedUpdate () {
    28.  
    29.         radians= angle * Mathf.Deg2Rad;
    30.        
    31.         if (flag)
    32.         {
    33.             t += 0.5F * Time.deltaTime;
    34.             x = startPosition.x + initialVelocity.x * Mathf.Cos(radians) * t;
    35.             y = startPosition.y + initialVelocity.y * Mathf.Sin(radians) * t - Physics.gravity.magnitude * Mathf.Pow(t, 2);
    36.             rgb.velocity = new Vector2(x, y);
    37.         }
    38.     }
    39.  
    40.  
    41. }
    42.  
    43.