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
0
Question by SoulQuick · Apr 09, 2019 at 02:42 PM · quaternionaddforceshootingknockbackaddforce movement

How am I able to move my (Player) character in the opposite direction of where I am shooting?

I want my player to move by shooting. Meaning that you get knocked back towards the opposite direction of where you're aiming and shooting. I am using this script to aim and shoot:

 public GameObject projectile;
 public Transform shotPoint;

 private float timeBtwShots;
 public float startTimeBtwShots;
 private void Update()
 {
     SetRotation();

     if (timeBtwShots <= 0)
     {

         if (Input.GetMouseButtonDown(0))
         {
             Instantiate(projectile, shotPoint.position, transform.rotation);
             timeBtwShots = startTimeBtwShots;
         }
     }
     else
     {
         timeBtwShots -= Time.deltaTime;
     }
 }

 private void SetRotation()
 {
     var pos = Camera.main.WorldToScreenPoint(transform.position);
     var dir = Input.mousePosition - pos;
     var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
 }

}

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

3 Replies

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

Answer by SoulQuick · Apr 17, 2019 at 01:28 PM

So I managed to get it working through the following line: "rb.AddForce((rb.gameObject.transform.position - shotPoint.position) * thrust);"

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Weapon : MonoBehaviour
 {
     //public float offset;
     public GameObject projectile;
     public Transform shotPoint;
 
     private float timeBtwShots;
     public float startTimeBtwShots;
     public Rigidbody2D rb;
     public float thrust;
 
     private void Update()
     {
         SetRotation();
 
         if (timeBtwShots <= 0)
         {
 
             if (Input.GetMouseButtonDown(0))
             {
                 Instantiate(projectile, shotPoint.position, transform.rotation);
                 Debug.Log("Rigid Body position: " + rb.position);
                 //rb.AddForce(-transform.right * thrust, ForceMode2D.Impulse);
                 rb.AddForce((rb.gameObject.transform.position - shotPoint.position) * thrust);
                 Debug.Log("Rigid Body position: " + rb.position);
                 timeBtwShots = startTimeBtwShots;
             }
         }
         else
         {
             timeBtwShots -= Time.deltaTime;
         }
     }
 
     private void SetRotation()
     {
         var pos = Camera.main.WorldToScreenPoint(transform.position);
         var dir = Input.mousePosition - pos;
         var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
     }
 }
 
Comment
Add comment · 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
0

Answer by Tecnophobe · Apr 09, 2019 at 06:25 PM

You can simply take the negative of transform.forward. You'll need to attach a rigidbody to your Object of course. Then you use AddForce with - transform.forward as the first parameter and Impulse mode for the second. Should look something like...

          if (Input.GetMouseButtonDown(0))
          {
              Instantiate(projectile, shotPoint.position, transform.rotation);
 
              rb.AddForce(-transform.forward * thrust, ForceMode.Impulse);
 
              timeBtwShots = startTimeBtwShots;
          }

rb = your rigidbody

thrust = a float number representing how hard you want the bullet to push (increase this number to push harder)

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 SoulQuick · Apr 11, 2019 at 01:12 PM 0
Share

I do have to add to this that I am not experienced in program$$anonymous$$g. So I tried this, but it is not working... Probably doing something wrong here ^^,

avatar image Tecnophobe SoulQuick · Apr 11, 2019 at 03:28 PM 0
Share

I'd be happy to help you figure it out. Can you share your updated file? It would help to have a screenshot of the values assigned to RB and thrust.

avatar image
0

Answer by SoulQuick · Apr 16, 2019 at 11:48 AM

@Tecnophobe Sorry for my late answer, but I basically put it where you put it. I even tried making the Rigidbody2D private and calling it in Start by saying "rb = GetComponent();

      public GameObject projectile;
      public Transform shotPoint;
      private float timeBtwShots;
      public float startTimeBtwShots;
      public Rigidbody2D rb;
      public float thrust;
      private void Update()
      {
          SetRotation();
          if (timeBtwShots <= 0)
          {
              if (Input.GetMouseButtonDown(0))
              {
                  Instantiate(projectile, shotPoint.position, transform.rotation);
          rb.AddForce(-transform.forward * thrust, ForceMode.Impulse);
                  timeBtwShots = startTimeBtwShots;
              }
          }
          else
          {
              timeBtwShots -= Time.deltaTime;
          }
      }
      private void SetRotation()
      {
          var pos = Camera.main.WorldToScreenPoint(transform.position);
          var dir = Input.mousePosition - pos;
          var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
          transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
      }
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 Tecnophobe · Apr 16, 2019 at 04:51 PM 0
Share

Ah, I see a problem. I thought your game was 3D. For a 2D game, you'll want to use transform.right. Try with that.

avatar image SoulQuick · Apr 16, 2019 at 06:33 PM 0
Share

It doesn't seem to work. Hm. Yea I kinda left out it's a 2D game. And also the Force$$anonymous$$ode had to be Force$$anonymous$$ode2D I believe.

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

106 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 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

How I can do knockback without OnColisson or OnTrigger 2 Answers

Add rotation to rigidbody AddForce? 3 Answers

Quaternion.Lerp messing up after being deactived and then activated again 1 Answer

How to apply knockback on collision (2D) 1 Answer

Rotation of Instantiated object wrong 3 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