Instantiating A group of platform and moving them down
so Guys below is my Code for My PlaycontrollerScript and PlatformMover script what i am trying to achieve here is i want when my player jump on the platform that particular group of platform to go down ,basically like doodle jump ,but as i am doing something wrong here platform groups are spawning perfectly but its not moving ,i am pretty new to things. can someone give me an idea of what i am doing wrong or guide me to a tutorial or something any help will be appreciated. Long Story Short what i want to do it after my Group of platform instantiate which is working fine i want to move it down when the player jumps on it ,i want all of them(group of platforms) to move down a step.
public GameObject[] easyGroups;
public GameObject currentGroup;
public GameObject nextGroup;
public GameObject startingPlatform;
public GameObject lastObjectJumped;
public int rand;
public int easyMin;
public int easyMax;
public Transform startingGroupTran;
public Transform topSpawnTran;
public Transform lowestTran;
public float speedDown;
public float toLerpDif;
public Vector3 toLerp;
// Use this for initialization
void OnEnable ()
{
FirstGroupFunction();
}
void Update()
{
if (nextGroup.transform.position.y <= 0)
{
newGroup();
}
groupMoveFunction();
}
void FirstGroupFunction()
{
rand = Random.Range(easyMin, easyMax);
currentGroup = Instantiate(easyGroups[rand], startingGroupTran.position, startingGroupTran.rotation) as GameObject;
rand = Random.Range(easyMin, easyMax);
nextGroup = Instantiate(easyGroups[rand], topSpawnTran.position, topSpawnTran.rotation) as GameObject;
nextGroup.transform.SetParent(currentGroup.transform);
startingPlatform.transform.SetParent(currentGroup.transform);
}
void groupMoveFunction()
{
currentGroup.transform.position = Vector3.Lerp(currentGroup.transform.position, toLerp, speedDown * Time.deltaTime);
}
// Update is called once per frame
void newGroup()
{
nextGroup.transform.SetParent(null);
Destroy(currentGroup);
currentGroup = nextGroup;
rand = Random.Range(easyMin, easyMax);
nextGroup = Instantiate(easyGroups[rand], topSpawnTran.position, topSpawnTran.rotation) as GameObject;
nextGroup.transform.SetParent(currentGroup.transform);
toLerpDif = lastObjectJumped.transform.position.y - lowestTran.position.y;
toLerp = new Vector3(0, currentGroup.transform.position.y - toLerpDif, 0);
}
}
public class Pandacontroller : $$anonymous$$onoBehaviour {
public float maxSpeed = 10f;
bool facingRight = true;
Animator anim;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public float jumpForce = 700f;
public Layer$$anonymous$$ask whatisGround;
public Platform$$anonymous$$over controller;
public string lastjump;
public GameObject panda;
public Rigidbody2D pandaRigid;
public bool isJumping;
public int jumptop;
public Vector3 toLerp;
// Use this for initialization
void OnTriggerEnter2d(Collider2D other)
{
if (other.tag == "Platforms")
{
isJumping = true;
pandaRigid.gravityScale = 0;
pandaRigid.velocity = Vector3.zero;
}
if(other.tag == "Platforms")
{
lastjump = "Platforms";
controller.lastObjectJumped = other.gameObject;
controller.toLerpDif = controller.lastObjectJumped.transform.position.y - controller.lowestTran.position.y;
controller.toLerp = new Vector3(0, controller.currentGroup.transform.position.y - controller.toLerpDif, 0);
}
}
void Start ()
{
anim = GetComponent<Animator>();
}
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatisGround);
anim.SetBool("Ground", grounded);
anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
float move = Input.GetAxisRaw("Horizontal");
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
anim.SetFloat("speed", $$anonymous$$athf.Abs(move));
//flip
if (move > 0 && !facingRight)
flip();
else if (move < 0 && facingRight)
flip();
}
void Update()
{
if (grounded && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
anim.SetBool("Ground", false);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
}
}
void flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Your answer
Follow this Question
Related Questions
clicks pass through UI elements on android 1 Answer
Unity Gradle Build Error 2019.3.10f 0 Answers
Can not load the next scene 0 Answers
Can't change an object Layers at runtime via c# code (Help Please) 0 Answers
Getting trouble in migrating the Unity IAP old version to new version of IAP in existing project 0 Answers