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

Question Read Transform forward direction in unity

Discussion in 'Scripting' started by Bannanaking3, Sep 8, 2023.

  1. Bannanaking3

    Bannanaking3

    Joined:
    Jun 21, 2022
    Posts:
    109
    I'm working on a top-down 2d Zelda-like game and I have a projectile that needs to go in the forward direction( or I guess y, locally to the object) when I tell it to using rigid body2d physics.

    Does anyone have a good method for moving objects in the forward direction in unity 2d? I don't even really need it to be a rigid body method, that just seemed easiest
     
  2. Bannanaking3

    Bannanaking3

    Joined:
    Jun 21, 2022
    Posts:
    109
    never mind, I found a solution.

    in fixed update:
    Code (CSharp):
    1.  
    2. //assign direction to a vector 2 to avoid confusing the computer, I guess
    3. Vector2 dir = new Vector2(transform.right.x, transform.right.y);
    4. //update position with this number and your speed variable
    5.         rb.position +=  dir * speed;
    6.  
     
  3. Bannanaking3

    Bannanaking3

    Joined:
    Jun 21, 2022
    Posts:
    109

    I spoke to soon, that's close but not perfect. why is this an issue?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    You will need to elaborate on this. Not perfect in what way?

    Note that this:
    Code (CSharp):
    1. Vector2 dir = new Vector2(transform.right.x, transform.right.y);
    Is kind of redundant, as it's exactly the same as:
    Code (CSharp):
    1. Vector2 dir = transform.right;
    As Vector3's get implicitly converted to Vector2.
     
    Bunny83 and angrypenguin like this.
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,503
    Why is what an issue? You haven't described how it's behaving, or how you expect it to behave.

    I have some questions from looking at that code, maybe one of them will help somewhere?

    1) If your 'dir' is just the X and Y components of transform.right, why aren't you just using transform.right? (Do you need to truncate it? If you do, I've a gut feel that you can just do Vector2 dir = transform.right; and it'll truncate the vector for you, but I don't use Vector2 enough to be sure of that.)

    2) Maybe this is your issue: is dir a unit vector? If it's not then dir * speed will not give you a vector with the magnitude of speed. I would expect that to be normalised.

    3) You're multiplying by speed, so why are you not multiplying by Time.deltaTime or similar?

    4) Do you realise that referencing transform.right twice is getting Unity to repeat a bunch of calculations twice? Personally I'd go Vector3 transformRight = transform.right; and then use that variable.
     
  6. Bannanaking3

    Bannanaking3

    Joined:
    Jun 21, 2022
    Posts:
    109

    You're absolutely right, I forgot a whole lot of details. my apologies, my brain is fried right now.

    the object in question is instantiated from a different script and is placed in a rotation facing its target. I'm trying to get it to travel in a straight line in the direction it is instantiated in.

    currently, the current iteration of code works great as long as the target object is to the right of the object I'm moving, otherwise it shoots off in exactly the opposite direction, and I'm not good enough at math to figure that out right now.


    in the case of assigning specifically to a new variable, that was an issue that had cropped up in a previous attempt at making this work and is entirely redundant in this situation, thanks for pointing that out.
     
  7. Bannanaking3

    Bannanaking3

    Joined:
    Jun 21, 2022
    Posts:
    109
    a little research brought me to this solution, which seems to work perfectly:

    set this in start:

    Code (CSharp):
    1.  
    2.  Vector2 projectileToTarget = target.position - transform.position;
    3.         dir = enemytoPlayer.normalized;
    4.  
    then use this to move the object in that direction:
    Code (CSharp):
    1.   rb.position +=  dir * speed;

    Edit: you could also set direction in update to make a homing projectile
     
  8. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,503
    That makes sense in principle, and I'm glad you've a working solution! However, some details are missing, so this won't make sense if someone else finds it looking to solve a similar problem.

    You calculate 'dir' by normalising a value 'enemyToPlayer'. That seems solid, but where does 'enemyToPlayer' come from? And what is 'projectileToTarget' used for?
     
  9. Bannanaking3

    Bannanaking3

    Joined:
    Jun 21, 2022
    Posts:
    109
    OH! My bad, I am very tired and neglected to change the name of the variable to make more sense.

    projectileToTarget and enemyToPlayer should be the same variable. It should look like this:

    Code (CSharp):
    1.  
    2. Vector2 projectileToTarget = target.position - transform.position;
    3.         dir = projectileToTarget.normalized;
    4.  
    projectile to target is the raw direction to be normalized for the "dir" variable.

    Sorry about that, that was super unclear. my bad.
     
    angrypenguin likes this.
  10. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,503
    Good stuff. And thanks for coming back to clarify.

    Do get some rest though, look after yourself!
     
  11. Bannanaking3

    Bannanaking3

    Joined:
    Jun 21, 2022
    Posts:
    109
    absolutely, thanks.
     
    angrypenguin likes this.