Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by MrPanda05 · May 26 at 04:20 AM · forceshootercrosshair

Trying to shoot object at a certing point via force

I'm trying to make a 2D game, where you are a mage and you can shoot on any direction you want. In my current situation, I want that the bullet will go to the crosshair direction, but its not working for me, it goes to anywhere except the cursor. Here my code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Shoot : MonoBehaviour
 {
     //Will shoot bullet relative to wand end point
     [Header("Vars")]
     [SerializeField]private Rigidbody2D Bullet;
     [SerializeField]private Transform ts;
     [SerializeField]private Transform barrell;
     [SerializeField]private Transform Player;
     [SerializeField]private float thrurst;
 
     //raiz(x2-x1)^2 + (y2-y1)^2
     private bool Mouse;
     float x1,x2, y1, y2, result, rootX, rootY;
 
     //Vector2 PlayerPos = new Vector2(Player.position.x, Player.position.y);
 
 
 
     private void Update()
     {
         GetMouseInput();
         if(Mouse)
         {
             var newBullet = Instantiate(Bullet, barrell.position, Quaternion.identity) as Rigidbody2D;
             
             float angle = GetAngle();
             var Force = findFuckingForces(angle);
             newBullet.AddForce(Force * thrurst, ForceMode2D.Impulse);
             //newBullet.AddRelativeForce(Vector3.right * thrurst, ForceMode2D.Impulse);
         }
     }
 
     void GetMouseInput()
     {
         Mouse = Input.GetMouseButtonDown(0);
     }
     //It is supposed to find the adjacent side and opposite side, then return it as Vector 2 to pass as instantiate
     Vector2 findFuckingForces(float angle)
     {
         //Gets the hypotenuse the shortest path to a point
         x1 = Player.position.x;
         x2 = ts.position.x;
         y1 = Player.position.y;
         y2 = ts.position.y;
 
         Vector2 Pos;
 
         rootX = Mathf.Pow((x2 - x1), 2);
         rootY = Mathf.Pow((y2 - y1), 2);
         result = Mathf.Sqrt(rootX + rootY);
         //acha cateto oposto e adjacente
         //cosseno cateto adjacente angulo = cateto adjacente / hipo
         //seno cateto oposto angulo = cateto oposto / hipo
         float coss = Mathf.Cos(angle);
         float seno = Mathf.Sin(angle);
 
         float catetoAdj = coss * result;
         float catetoOpst = seno * result;
         Pos = new Vector2(catetoOpst, catetoAdj).normalized;
         return Pos;
     }
     //it is Supposed to return an angle between end of wand and where the crosshair is
     float GetAngle()
     {
         x1 = Player.position.x;
         x2 = ts.position.x;
         y1 = Player.position.y;
         y2 = ts.position.y;
         
         float angle = Mathf.Atan2(y2 - y1, x2 - x1) * 180 / Mathf.PI;
         Debug.Log(angle);
         return angle;
     }
     
 }
 

` I tried using some trigonometry but I don't know if I did it right.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by WolverineLight · May 27 at 03:48 PM

Hello MrPanda05,

Firstly I would suggest NOT using AddForce, or any kind of active physics like this, to propel a bullet. It is possible, however. Some alternatives include A) RayCast, which can be used to scan for a bullet impact, or B) is to continually move the bullet every frame. If you game requires a physics-based bullet, it may be best to do the originall physics-based approach.

Secondly, as a solution, there are a few ways to approach this.

Using physics, you can do the following:

  • Create two empty gameobjects.

  • Place one to be a child of the main camera. This will act as an origin.

  • Place the other in the direction you wish to aim, (also on the main camera).

  • Use the positions of both of these to create a direction. (Simply subtract the positions of the two for a direction).

  • To fire, instantiate the bullet at the origin. Add a force in the direction you calculated above. No trig necessary.

Using raycast: - Simply create a raycast:

 var ray = camera.ScreenPointToRay (Vector3 (Screen.width/2,Screen.height/2, 0));
  • Check for a collision:

var hit : RaycastHit; if (Physics.Raycast (ray, hit, 100)) { distance = hit.distance; collider1= hit.collider; Debug.Log(distance); Debug.DrawLine(transform.position, hit.point, Color.red); }

If you want to move the bullet slowly, simply moving it every frame might work.

You can also break raycast into sections if you want to have the bullet travel over time as well.

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

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

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

Why Rigidbody.AddForce is not acting like the correct physical force? 1 Answer

Rigidbody bullet force not being applied 0 Answers

Increase jump speed colliding on a box ?? 1 Answer

pulling gameobjects across collider C# 3 Answers

How to jump using a specific force/velocity on y? 0 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