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

Resolved Issues with AddForce on negative x-Axis

Discussion in 'Physics' started by hardlySmoker, Feb 5, 2021.

  1. hardlySmoker

    hardlySmoker

    Joined:
    Feb 3, 2021
    Posts:
    2
    Hi,

    Im fairly new to Unity but not to Programming. I have some issues with AddForce on a 3d Rigidbody

    Im playing around with a simple 3d Game where you control a stationary canon in the middle. with the left and right keys you can rotate the canon on the global z-axis (local y-axis). if the canon is not moved it starts shooting. (Thats where i got so far so no enemies are implemented yet)

    Currently everything works as it should except for some (NOT ALL!!) rotations towards the negative x-axis (global). When i log the vector it outputs the correct values but it seems like the x coordinates are being ignored. That means the bullets go straight up or down according to the y value.

    For the bullet Rigidbody i turned off gravity and drag in case that could be an issue.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerScript : MonoBehaviour
    6. {
    7.     public GameObject bullet;
    8.     public Transform bulletSpawn;
    9.     public float shootRate = 0f;
    10.     public float shootForce = 0f;
    11.     private float shootRateTimeStamp = 0f;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.      
    17.     }
    18.  
    19.     void shoot()
    20.     {
    21.         GameObject bulletClone = Instantiate(bullet);
    22.         bulletClone.transform.position = bulletSpawn.position;
    23.  
    24.         Debug.Log(transform.forward * shootForce);
    25.         bulletClone.GetComponent<Rigidbody>().AddForce(transform.forward * shootForce);
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         if (!Input.anyKey)
    32.         {
    33.             if (Time.time > shootRateTimeStamp)
    34.             {
    35.                 shoot();
    36.                 shootRateTimeStamp = Time.time + shootRate;
    37.             }
    38.         }
    39.  
    40.         if (Input.GetKey("left"))
    41.             transform.Rotate(0, -10, 0);
    42.         if (Input.GetKey("right"))
    43.             transform.Rotate(0, 10, 0);
    44.     }
    45. }
    Thanks in advance,
    Jay
     
    Last edited: Feb 5, 2021
  2. hardlySmoker

    hardlySmoker

    Joined:
    Feb 3, 2021
    Posts:
    2
    Well, i solved it! There was a problem with collision between the bullet and the canon. I Turned off collision for the barrel of the canon since i dont need that anyways and no it works. still i dont get why only some negative x values have this issue. Any ideas?