Problem is not reproducible or outdated
Move parent without moving the child
I am new to Unity but I know that if an object is a child, then it will follow the parent and have the same rotations; however, I am creating a top-down, 2d, mobile, naval warfare game and I wanted to instantiate the cannonball prefab with the push of the button (that works) and then in a script on the cannonball prefab I had the cannonball check what the tag of its parent was (left side cannon or right side cannon) to know whether to move left or right (with transform.Translate). Basically, I wanted to know if I could make the cannonball move independently from the parent while still being a child of that parent.
More info: I have a ship game object that has 4 children (right side cone of fire, left side cone of fire, right side cannon game object to spawn the cannonballs, and left side cannon that does the same), the lines that have R_Cannon, L_Cannon, etc. are the cannons on the fort that the player is shooting, also whenever I look at a fort cannon gameObject and I press the shoot button, it gives me the error "NullReferenceException: Object reference not set to an instance of an object CannonFire.Update () (at Assets/Scripts/CannonFire.cs:37)" line 37 is the one that has == "R_Cannon" . Here's the code:
public class CannonFire : MonoBehaviour {
[SerializeField] private float canBallSpeed = 2f;
[SerializeField] private GameObject explosionPrefab;
[SerializeField] private GameObject ripplePrefab;
[SerializeField] private GameObject ship;
private float fortCannonDamage = 10;
private bool rSideFired = false;
private bool lSideFired = false;
// Use this for initialization
void Start () {
StartCoroutine(CannonballLifespan());
if (ship.GetComponent<PlayerBehavior>().rSideCanball) {
gameObject.transform.eulerAngles = ship.transform.GetChild(2).transform.eulerAngles;
rSideFired = true;
}
if (ship.GetComponent<PlayerBehavior>().lSideCanball) {
gameObject.transform.eulerAngles = ship.transform.GetChild(3).transform.eulerAngles;
lSideFired = true;
}
}
// Update is called once per frame
void Update () {
if (gameObject.transform.parent.name == "R_Cannon") {
transform.Translate(Vector2.right * Time.deltaTime * canBallSpeed);
}
if (gameObject.transform.parent.name == "L_Cannon") {
transform.Translate(Vector2.left * Time.deltaTime * canBallSpeed);
}
if (gameObject.transform.parent.name == "U_Cannon") {
transform.Translate(Vector2.up * Time.deltaTime * canBallSpeed);
}
if (gameObject.transform.parent.name == "D_Cannon") {
transform.Translate(Vector2.down * Time.deltaTime * canBallSpeed);
}
/*if (gameObject.transform.parent.tag == "Right Side Cannons") {
gameObject.transform.parent = null;
transform.Translate(Vector2.left * Time.deltaTime * canBallSpeed);
}
if (gameObject.transform.parent.tag == "Left Side Cannons") {
gameObject.transform.parent = null;
transform.Translate(Vector2.right * Time.deltaTime * canBallSpeed);
}*/
if (rSideFired == true)
{
RightSideFired();
}
if (lSideFired == true)
{
LeftSideFired();
}
}
void RightSideFired ()
{
transform.Translate(Vector2.left * Time.deltaTime * canBallSpeed);
}
void LeftSideFired ()
{
transform.Translate(Vector2.right * Time.deltaTime * canBallSpeed);
}
Answer by tormentoarmagedoom · Jun 27, 2018 at 02:50 PM
Good day.
Whats the question then? the error?
This errors must be solved by you. You have 1000 Unity answers/Videos/Youtubes/Manuals about this so learn to solve them.
And if need help with the rotation question, please make it more "fluid" and "easy" to understand what are you asking.
Thanks and Bye! Waiting you question!
Sorry I confused the question a little, but I was able to figure it out the problem that I was having.
I wanted to know how to move the child independently from the parent, but I ended up not making it a child. Again, my apologies for the confusion.
Follow this Question
Related Questions
Vector3.Lerp not working properly, making the player bounce around 2 Answers
A way to make the player get punished when standing still? 0 Answers
Randomly set an integer as positive or negative? 1 Answer
How to change rotation for a FirstPersonController through script. 0 Answers
How can i make the right choice of implementation of my shooting fire script ? 1 Answer