Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by FuryFight3r · Jul 13, 2017 at 10:28 PM · rigidbodyaddforceaddrelativeforce

AddForce and AddRelativeForce not forcing in the right directions

Hey there guys, I am having an issue with the directions of force being produced by a rigidbody. I am trying to make a gun that shoots a bullet casing out the right of the gun, but when happens is it always shoots to the right of the world space, so if i spin 180° the bullet casing is now being shot of of the left side of the gun, i have provided a detailed video clip to both explain what im trying to achieve and show what has previously been suggested, but does not work.

https://youtu.be/43mQRbDgMEM

My Coding

Bullet Casing Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class caseEjector : MonoBehaviour {
 
     float thrust = 250;
     float flickThrust = 0.1f;
 
     public GameObject bulletCase;
 
     Rigidbody rig;
 
     public void shoot()
     {
         GameObject cap = Instantiate(bulletCase, transform.position, Quaternion.Euler(0, 0, 0));
         cap.transform.parent = gameObject.transform;
         rig = cap.GetComponent<Rigidbody>();
         rig.AddRelativeForce(new Vector3(1, 0.1f, 0).normalized * thrust);
         cap.transform.parent = null;
     }
 }
 

Gun Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TestShoot : MonoBehaviour {
 
     public bool canShoot;
     public bool reloading;
 
     public float bulletClip = 17;
     float origClip;
 
     float shotTimer;
     float startRTimer;
     float reloadTimer = 1.5f;
 
     Animation anim;
     public AudioClip shotFired;
     public AudioClip reloadSound;
     public AudioClip emptyClip;
     AudioSource aud;
 
     public GameObject caseEjectorGO;
     public Transform casing;
     public Transform casePos;
 
     public Vector3 shootLoc;
     public Transform bullet;
     public Transform shootPos;
 
     void Start () {
         origClip = bulletClip;
         startRTimer = reloadTimer;
         aud = this.GetComponent<AudioSource>();
         anim = this.GetComponent<Animation>();
         canShoot = true;
         reloading = false;
     }
 
     void Update () {
         shotTimer += Time.deltaTime;
 
         if(shotTimer <= 0.4f)
         {
             canShoot = false;
         }
         else
         {
             canShoot = true;
         }
 
         if (canShoot && bulletClip > 0)
         {
             if (Input.GetButtonDown("Shoot"))
             {
                 anim.Play("APCFire");
                 aud.PlayOneShot(shotFired);
                 bulletClip -= 1;
                 shotTimer = 0;
                 shootLoc = shootPos.transform.position;
                 caseEjectorGO.GetComponent<caseEjector>().shoot();
                 Instantiate(bullet, shootLoc, Quaternion.Euler(0, 90, 0));
             }
         }
         if (bulletClip <= 0 && canShoot)
         {
             if (Input.GetButtonDown("Shoot"))
             {
                 aud.PlayOneShot(emptyClip);
                 shotTimer = 0;
             }
         }
 
         if (Input.GetButtonDown("Reload"))
         {
             aud.PlayOneShot(reloadSound);
             canShoot = false;
             reloading = true;
             anim.Play("APCReload");
         }
 
         if (reloading)
         {
             reloadTimer -= Time.deltaTime;
             if(reloadTimer <= 0)
             {
                 bulletClip = origClip;
                 reloading = false;
                 canShoot = true;
                 reloadTimer = startRTimer;
             }
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jinkata · Jul 13, 2017 at 10:42 PM

on this line in your CaseEjector

 rig.AddRelativeForce(new Vector3(1, 0.1f, 0).normalized * thrust);

try this instead

 rig.AddForce((this.transform.right + (Vector3.up * 0.1f)).normalized * thrust);

this will apply a force to the 'right' direction of the whatever GameObject this script is on plus an up force on the y axis of 0.1f. I'm assuming that this script is on a GameObject that has it's forward facing toward the front of the gun and the up facing to the top of the gun.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FuryFight3r · Jul 13, 2017 at 10:45 PM 0
Share

Thank you for the reply, but this still shoots only to the right of the world, in no way does it follow the rotation of the character and gun.

avatar image Jinkata FuryFight3r · Jul 13, 2017 at 10:47 PM 0
Share

I updated it. I meant to change the AddRelativeForce to simply AddForce

avatar image FuryFight3r · Jul 13, 2017 at 10:51 PM 0
Share

Still no difference, this was the case i had in my previous question, AddForce and AddRelativeForce. made no noticeable difference at all

EDIT: i just noticed you changed it to rig.AddForce((this.transform.right + (Vector3.up * 0.1f)).normalized * thrust);

i have implemented this and it works, but is shooting backward, i will fix the direction it shoots from and update you on how that goes! looking good! thank you!

avatar image FuryFight3r · Jul 13, 2017 at 11:08 PM 0
Share

Thanks mate! this was what i needed was just the this.transform, i just had no idea in how to implement in to actually take the 'this' axis but thankyou you pointed me in the direction i was looking for. i used this to fix it.

rig.AddForce((this.transform.right).normalized * thrust);

(i had removed the Vector3.up due to the caseEjectorGO handles the angles, i just had to spin it 90° to it could take right as actually being right)

Thank you very much for you input i have awarded you the correct answer

avatar image Jinkata FuryFight3r · Jul 14, 2017 at 12:52 AM 0
Share

Right on. Glad myself and @Bunny83 could help out. I upvoted their answer as well since it's very important to keep in $$anonymous$$d the orientation of the GameObjects.

avatar image
2

Answer by Bunny83 · Jul 13, 2017 at 10:55 PM

Your actual problem is that you do not rotate your instantiated object the way your gun looks.

 GameObject cap = Instantiate(bulletCase, transform.position, Quaternion.Euler(0, 0, 0));

You rotate the cap object to the default identity rotation. Parenting the object does keep the worldspace orientation.

There are two solutions to rotate your "cap" the same way the case ejector looks.

First use the new SetParent with the second parameter set to false. That will keep the local orientation and carry it over to the new space. This will change the worldspace orientation.

The second solution is to directly instantiate the object with the right rotation. For example:

 GameObject cap = Instantiate(bulletCase, transform.position, transform.rotation);

Note that you have to use only one of the two solutions.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FuryFight3r · Jul 13, 2017 at 11:10 PM 0
Share

This was my next problem I was facing, so thank you for providing this input, i have simply remove the Quaternion and replaced it with transform.rotation to take the rotation of the caseEjector attached the the gun! Thanks again!

avatar image Bunny83 FuryFight3r · Jul 13, 2017 at 11:44 PM 0
Share

That also solves your initial problem with the force. Since you apply a local space force the object always moved in the same direction since the object was always rotated the same way.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding a force to an object relative to a rotation? 0 Answers

A sphere moved by relativeforce is acting wierd? 2 Answers

Rigid body rotation question 1 Answer

Bow and Arrow functionality in Unity3d 0 Answers

Projectiles always going to the left and not forward 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges