Auto growing / scaling - C#
Hi guys,
The situation is following: I've got a player in the room. The player can move and jump. Each time a player jumps up and gets higher- walls should grow up with him, not to let him ever reach the top.
Can anyone recommend, how to scale walls properly based on player position? I have tried the method below, but it's too complicated and doesn't work properly, making me thinking, that there should be some clever and easy solution....
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WallGrowing : MonoBehaviour {
public GameObject LeftWall;
public GameObject RighWall;
public GameObject FrontWall;
public GameObject BackWall;
public GameObject UpperLimit;
public GameObject player;
private float PlayerPositionY;
private float UpperLimitPositionY;
private float LeftWallCurrentScaleY;
private float difference;
private float ULSSpawnPositionX;
private float ULSSpawnPositionY;
private float ULSSpawnPositionZ;
private Vector3 ScaleChange = new Vector3(0.01f, 0, 0);
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//*
SnapCurrentHeight();
HeightChange();
UpperLimitSpawn();
Debug.Log(PlayerPositionY + " " + UpperLimitPositionY + " " + difference);
//*/
}
//*
void UpperLimitSpawn()
{
ULSSpawnPositionX = UpperLimit.transform.position.x;
ULSSpawnPositionX = UpperLimit.transform.position.y;
ULSSpawnPositionX = UpperLimit.transform.position.z;
if (difference < 3)
{
float ULSSpawnPositionYNew = ULSSpawnPositionY + 5;
UpperLimit.transform.position = new Vector3(ULSSpawnPositionX, ULSSpawnPositionYNew, ULSSpawnPositionZ);
}
}
void SnapCurrentHeight()
{
PlayerPositionY = player.transform.position.y;
UpperLimitPositionY = UpperLimit.transform.position.y;
LeftWallCurrentScaleY = LeftWall.transform.localScale.x;
difference = UpperLimitPositionY - PlayerPositionY;
}
void HeightChange()
{
if (difference < 3)
{
WallScaneTransformPlus();
}
if (difference > 2 && difference < 5)
{
WallScaneTransformPlusMinus();
}
}
void WallScaneTransformPlus()
{
LeftWall.transform.localScale += Vector3.up + (ScaleChange/2);
LeftWall.transform.Translate(Vector3.right / 45);
}
void WallScaneTransformPlusMinus()
{
LeftWall.transform.localScale += Vector3.up - (ScaleChange/2);
LeftWall.transform.Translate(Vector3.left / 45);
}
// */ }
Answer by kozlovskiygk · Nov 02, 2021 at 10:51 AM
If somebody is following, I am trying another way now: I have attached walls to the player, so they are constantly moving and it looks cool + the idea is to instantiate walls again and again when some Y is reached. But this is to be pollished.
= using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WallGrowing : MonoBehaviour {
public GameObject LeftWall;
public GameObject RighWall;
public GameObject FrontWall;
public GameObject BackWall;
public GameObject UpperLimit;
public GameObject player;
public float playerPositionY;
public float oldlWallYposition;
private float newWallXposition;
public float newWallYposition;
private float newWallZposition;
public GameObject[] wallPrefab;
// Start is called before the first frame update
void Start()
{
oldlWallYposition = LeftWall.transform.position.y;
newWallXposition = LeftWall.transform.position.x;
newWallZposition = LeftWall.transform.position.z;
}
// Update is called once per frame
void Update()
{
SnapPlayerPosition();
SpawnChecker();
Debug.Log(playerPositionY);
}
void SpawnChecker() { if (playerPositionY > 10) { WallSpawn(); } }
void SnapPlayerPosition() {
playerPositionY = player.transform.position.y;
}
void WallSpawn() {
newWallYposition = oldlWallYposition + 10;
Vector3 spawnPosWall = new Vector3(newWallXposition, newWallYposition, newWallZposition);
Instantiate(wallPrefab[0], spawnPosWall, wallPrefab[0].transform.rotation);
oldlWallYposition = newWallYposition;
}
}
and the final solution is this: (thank to all of you, lol :D)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WallGrowing : MonoBehaviour {
public GameObject LeftWall;
public GameObject RighWall;
public GameObject FrontWall;
public GameObject BackWall;
public GameObject UpperLimit;
public GameObject player;
public float playerPositionY;
public float oldlWallYposition;
private float newWallXposition;
public float newWallYposition;
private float newWallZposition;
public GameObject[] wallPrefab;
private float level = 0;
public float $$anonymous$$X = 5f;
public float x1;
// Start is called before the first frame update
void Start()
{
oldlWallYposition = LeftWall.transform.position.y;
newWallXposition = LeftWall.transform.position.x;
newWallZposition = LeftWall.transform.position.z;
}
// Update is called once per frame
void Update()
{
SnapPlayerPosition();
levelChecker();
x1 = player.transform.position.y;
Debug.Log(x1 +" " + "$$anonymous$$X=" + $$anonymous$$X + " " + "level=" + level);
}
void levelChecker() { if (x1 > $$anonymous$$X) { level += 1; $$anonymous$$X = $$anonymous$$X + 5; WallSpawn(); } }
void SnapPlayerPosition() {
playerPositionY = player.transform.position.y;
}
void WallSpawn() { newWallYposition = oldlWallYposition + 5; Vector3 spawnPosWall = new Vector3(newWallXposition, newWallYposition, newWallZposition);
Instantiate(wallPrefab[0], spawnPosWall, wallPrefab[0].transform.rotation);
oldlWallYposition = newWallYposition;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613044910im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Two Raycast2D on object cancel each other out 0 Answers
Unity Starting with White Screen 0 Answers
Value doesn't change in game when updated. 2 Answers
How to Fix my Unity? 0 Answers
Unity wont install any versions? 0 Answers