Jump to content
  • Advertisement
Sign in to follow this  
srikanth chitturi

Unity How to create Obstacles dynamically before the player like in Temple run.

This topic is 2155 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello ,
I have created a player that runs forever until it hits any obstacle. The game has some buildings,cars .,etc. Now I want to spawn some Obstacles dynamically before the player.
To do this I have created an Empty GameObject called PlayerFollower and added it as a Child to the Player GameObject. So when ever the player moves forward the PlayerFollower also moves.
The script I have attached to the Player is :
using UnityEngine;
using System.Collections;
public class playerFollower : MonoBehaviour {
public Transform Player;
private GameObject Obstacle;
public GameObject Hurdles;
private Transform rotation;
// Use this for initialization
void Start () {
InvokeRepeating("createHurdles", 2, 7.0F);
void createHurdles()
Vector3 playerPos = Player.position;
Vector3 ObstaclePosition = transform.position;
ObstaclePosition.y = 1;
Obstacle = Instantiate(Resources.Load("Obstacles/Hurdle"),ObstaclePosition,transform.rotation) as GameObject;
Obstacle.transform.rotation = Player.transform.rotation;
Debug.Log("rotation :"+Obstacle.transform.rotation);
Obstacle.transform.parent = Hurdles.transform;
Debug.Log("Hurdle created at :"+ObstaclePosition);
// Update is called once per frame
void Update () {
float dist = Vector3.Distance(Player.position,transform.position);
Debug.Log("Distance :"+dist);
Also I want to destroy the Obstacle after the player has crossed it. I want to do this in the Update function above :
If(dist <= 3.0f)
But this is not an effective solution , because the distance is sometimes not less than 3.0f and if I give something like >5.0f , The Obstacle destroys in advance, before the player . Is there any better solution.
Also there are buildings , in behind , If i move the PlayerFollower also moves and it is creating Obstacles inside the buildings , how to overcome this ?

Share this post

Link to post
Share on other sites

You should check if the object is behind the player (dot product) , and then check the distance before destroying objects.  That way you can only destroy objects that are behind the player.  Though instead of doing a distance check, you may want to check if they are off camera instead.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!