Search Unity

Unity 2D - Rotating Child Object

Discussion in '2D' started by Lemonofdoom2, Feb 6, 2020.

  1. Lemonofdoom2

    Lemonofdoom2

    Joined:
    Aug 26, 2019
    Posts:
    5
    Hi there, trying to do a 2D platformer where the player can launch objects and so I have a firePoint near the player character. When the player is facing to the right, the firepoint is also facing right. Perfect.

    However, when the player faces left the firepoint is facing right. Not perfect.
    I've tried different stuff over the past week to get it to work on my own including digging through past unity forums questions to no avail. I would love any and all advice at this point.

    I've included the codes for both the Run and FlipSprite methods.


    Code (CSharp):
    1. private void Run()
    2.     {
    3.         float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal"); //value is between -1 to 1
    4.         Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidBody.velocity.y);
    5.         myRigidBody.velocity = playerVelocity;
    6.  
    7.         bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
    8.        
    9.             if(myFeet.IsTouchingLayers(LayerMask.GetMask("Ground")) && playerHasHorizontalSpeed)
    10.         {
    11.             myAnimator.SetBool("isRunning", true);
    12.         }
    13.  
    14.         else
    15.         {
    16.             myAnimator.SetBool("isRunning", false);
    17.         }
    18.     }
    19.  
    20.  
    21.     private void FlipSprite()
    22.     {
    23.         bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
    24.         if (playerHasHorizontalSpeed)
    25.         {
    26.             transform.localScale = new Vector2(Mathf.Sign(myRigidBody.velocity.x), 1f);
    27.         }
    28.     }
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Is your fire point a child object of your player?
     
  3. Lemonofdoom2

    Lemonofdoom2

    Joined:
    Aug 26, 2019
    Posts:
    5
    Yeppers!
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Then write the same code that flips your player to flip the fire point. You may also want to change the rotation of your fire point by -180 degrees on the X-axis. That way your bullets will spawn in the opposite direction.
     
  5. Lemonofdoom2

    Lemonofdoom2

    Joined:
    Aug 26, 2019
    Posts:
    5
    Well, I might be a bit of slow learner and a moron all combined because I have tried that to no avail. I literally have no idea what I am doing and at this point are more likely to throw my laptop through the wall then get this code to work the way I envision. Here is what I have for the firePoint script

    I had tried using quaterion at one point with logic statements for if crossplatform input was receiving a 1 or a -1 but that also didn't work so I have no clue on what I am doing wrong. I commented out that quaterion in the meanwhile. Junk code to be deleted.:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityStandardAssets.CrossPlatformInput;
    5.  
    6. public class firePoint : MonoBehaviour {
    7.  
    8.  
    9.     Rigidbody2D myRigidBody;
    10.  
    11.     [SerializeField] float runSpeed = 5f;
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         myRigidBody = GetComponentInParent<Rigidbody2D>();
    17.        
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.         FlipSprite();
    23.     }
    24.  
    25.  
    26.     private void FlipSprite()
    27.     {
    28.  
    29.         float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal"); //value is between -1 to 1
    30.         Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidBody.velocity.y);
    31.         myRigidBody.velocity = playerVelocity;
    32.  
    33.        
    34.         bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
    35.         if (playerHasHorizontalSpeed)
    36.         {
    37.             transform.localScale = new Vector2(Mathf.Sign(myRigidBody.velocity.x), 1f);
    38.             //transform.localRotation = new Quaternion(0f,180f,0f,0f);
    39.         }
    40.     }
    41. }
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You're not a slow learner, you're just impatient. You can't expect to magically understand everything up front or by intuition. Instead of taking random guesses, giving up, or getting someone else to write code for you, this is the time to debug your code and learn from your mistakes so you won't make them as often.

    The ideal option is to use an IDE like Visual Studio Community, where you can set breakpoints in your code, and attach the debugger to Unity. The game will pause at a breakpoint and let you step through your code line by line and see what the values are. This is an essential tool to understand, and I recommend learning about it and using it.
    https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    Even without the debugger, you have plenty of options. You say that you don't know where your code is going wrong, so look and see where it is going wrong. Do some trial and error. Comment out some lines and make sure that each line is doing what it is supposed to do.

    Anything you want to know the value of during gameplay, you can use Debug.Log to print it out to the console. Or make a new public field and set that value so you can read it in the inspector. You could put a Debug.Log after every line to verify the values if you wanted to.

    Don't understand Quaternions? Unity has a very nice tutorial section for that.
    https://learn.unity.com/tutorial/quaternions

    TLDR: Stop trying to fix your code before you even understand how it's broken.
     
  7. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Jeffrey Scotch said it best. You will discover that each new coding endeavor you try to do, you will encounter a multitude of issues. You are going to design code that later conflicts and it will ruin your program for the most part. However, this is how I learned and I'm sure everyone else as well. You must have patience. Everyone has been at the "throw the laptop at the wall moment" lol

    Anyway, here's a decent video to show you a lot about flipping the player.