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

Trying to add force to velocity at an angle

Discussion in 'Scripting' started by nathukadevyt, Jul 17, 2021.

  1. nathukadevyt

    nathukadevyt

    Joined:
    Mar 28, 2021
    Posts:
    17
    Hello everyone, I'm currently making a 2D Shoot-To-Move Platformer And I Am wondering how I should go about adding force to the velocity at the angle defined in my mouse Vector. If anyone knew a simple way to do this then that would be greatly appreciated!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GunScript : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     public float VelocityIncrease;
    9.  
    10.     public Rigidbody2D playerrb;
    11.  
    12.     public Transform gunBody;
    13.     public Transform FiringPoint;
    14.  
    15.     public KeyCode buttonToShoot;
    16.  
    17.     void Update()
    18.     {
    19.         Vector3 mouseScreen = Input.mousePosition;
    20.         Vector3 mouse = Camera.main.ScreenToWorldPoint(mouseScreen);
    21.  
    22.         gunBody.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x) * Mathf.Rad2Deg - 90);
    23.  
    24.         Shooting();
    25.     }
    26.  
    27.     void Shooting()
    28.     {
    29.         if (Input.GetKeyDown(buttonToShoot))
    30.         {
    31.             Vector2 gunKnockback = new Vector2(playerrb.velocity.y, VelocityIncrease);
    32.             playerrb.velocity = gunKnockback;
    33.         }
    34.     }
    35. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    It is going to be something like the above but because of how line 22 is written all as one horrible long mess, it's not possible to reason where it might be going wrong.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    Once you have done that, to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. nathukadevyt

    nathukadevyt

    Joined:
    Mar 28, 2021
    Posts:
    17
    Well although Line 22 is pretty messy, your answer is completely unrelated to what I'm asking. I'm not trying to fix an error, but am instead asking for information on how I should approach embedding a system where the velocity of a Rigidbody can be changed depending on a Vector2 angle. Your copy + pasted answer isn't much help to me here.
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    If what I assume you're asking is correct, then you can get the forward direction of the bullet using something like
    bullet.transform.forward
    to get a direction vector, and then add force based on that.
     
  5. nathukadevyt

    nathukadevyt

    Joined:
    Mar 28, 2021
    Posts:
    17
    Thanks For Your Help!