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

Problem with Prefab direction! Help please!

Discussion in '2D' started by Syzo, Nov 1, 2016.

  1. Syzo

    Syzo

    Joined:
    Nov 1, 2016
    Posts:
    17
    Hey! For some days I had a problem regarding one prefab direction, basically I'm building a 2D shooter game where the gun follows the mouse cursor.

    My current problem is, when the player is facing right the bulletPrefab goes in the direction I want, but when I flip the character the bulletPrefab goes in the opposite direction... I tried to change it manually and it seems to be a problem with the rotation but I don't know how to solve it. Can you guys help? What I have:

    Code (CSharp):
    1. voidShoot(){
    2. // Obter posição do rato no "mundo"
    3. Vector2 mousePostion =newVector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
    4. Vector2 firePointPosition =newVector2(firePoint.position.x, firePoint.position.y);
    5. RaycastHit2D hit =Physics2D.Raycast(firePointPosition, mousePostion-firePointPosition,100, whatToHit);
    6. if(Time.time >= timeToSpawnEffect)
    7. {
    8. Effect();
    9. timeToSpawnEffect =Time.time +1/effectSpawnRate;
    10. }
    11. Debug.DrawLine(firePointPosition,(mousePostion-firePointPosition)*100);
    12. if(hit.collider !=null){
    13. Debug.DrawLine(firePointPosition, hit.point,Color.red);
    14. Debug.Log("Hit:"+ hit.collider.name);
    15. }
    16. }
    17. voidEffect()
    18. {
    19. Transform playerTransform =GameObject.FindGameObjectWithTag("Player").transform;
    20. Vector2 playerScale = playerTransform.localScale;
    21. if(playerScale.x >0)
    22. {
    23. Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation);
    24. }
    25. else
    26. {
    27. Instantiate(BulletTrailPrefab, firePoint.position,Quaternion.Euler(firePoint.rotation.x, firePoint.rotation.y,-firePoint.rotation.z));
    28. }
    29. }
    Image of what's happening (when !facingRight):
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Whichever direction you're using to point the gun at the mouse, use that direction as the forward direction for the bullet.

    What is "firePoint"? If that is a child object at the end of the gun, you could also use that object's local axis as the direction for the bullet. Use whichever axis is pointing at the mouse. If it's the X axis, you can use "firePoint.right", if it's Y axis, "firePoint.up". (assuming firePoint is a Transform reference).
     
  3. Syzo

    Syzo

    Joined:
    Nov 1, 2016
    Posts:
    17
    Yes, "firePoint" is a child object of the player and it's located in the tip of the gun...
    Could you give me an example of what you're saying? in which cases can I use .right or .up?
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    If you have firePoint as a child of the rotating arm, pointed in the same direction as the gun, then you should only need to use the first Instantiate line:
    Code (CSharp):
    1. Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation);
    The child object should always point along the gun, even if you change the parent's scale and rotation.

    What happens when you use that line for both cases?
     
  5. Syzo

    Syzo

    Joined:
    Nov 1, 2016
    Posts:
    17
    Thats what I had before, this happens:


    when facingRight it works fine
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    I'm not entirely certain why you're seeing that result. Try this:

    Code (CSharp):
    1. GameObject bulletTrail = Instantiate(BulletTrailPrefab);
    2.  
    3. bulletTrail.transform.position = firePoint.position;
    4. bulleTrail.transform.right = firePoint.right;
    That should always match your bullet trail position to your firePoint's position and bullet trail's X axis to the firePoint's X axis. If you're using Y axis as forward then use "up" instead of "right".

    I tend to prefer to set local axes as directions instead of messing with rotations if I don't have to. Seems more intuitive to me.

    You could also use the Arm object's pointing axis as well considering that is always pointing the correct direction as you have it.

    Also, the problem with your original code sample was that "rotation" doesn't contain euler angles. This is what you were going for, not sure if it will work or not though.
    Code (CSharp):
    1. Instantiate(BulletTrailPrefab, firePoint.position, Quaternion.Euler(firePoint.eulerAngles.x, firePoint.eulerAngles.y, -firePoint.eulerAngles.z));
     
    Last edited: Nov 1, 2016
  7. Syzo

    Syzo

    Joined:
    Nov 1, 2016
    Posts:
    17
    Not working also...
    Someone else suggested that I should do Instantiate(BulletTrailPrefab, firePoint.position, Quaternion.LookRotation(-firePoint.forward));

    but the bullet always go forward now
     
  8. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    "forward" is the Z axis, and would normally be the direction the camera is looking.

    It seems like there's something different about the way you set up your hierarchy. Which axes does your character move along?

    Here, this recalculates the direction towards the mouse, which you seem to have already calculated, but your firePoint is not respecting.

    Code (CSharp):
    1. GameObject bulletTrail = Instantiate(BulletTrailPrefab);
    2. bulletTrail.transform.position = firePoint.position;
    3.  
    4. Vector3 screenDirection = Input.mousePosition - Camera.main.WorldToScreenPoint(firePoint.position);
    5. bulletTrail.transform.right = screenDirection;
    This assumes the bulletTrail's "forward" axis is X.

    If I were you I would look in the Scene view while the game is running and see what "firePoint" is doing. Is it rotating at all when the arm rotates?