- Home /
how to stop my scene on collision..
Hi! i have script in which we create the endless track , 1st end 2nd starts, 2nd end then 1st start again in a linklist... and these tracks are moving toward my player which seems like it's running. what i want is on collision stop the track movement and player play an animation. in my below script "foreach(Transform road in roads) { road.Translate(0,0, -10f * Time.deltaTime); }" this one stop and start the movement .... kindly help me out please :( ... i am not good in c# ....
using UnityEngine;
using System.Collections.Generic;
public class MoveRoad : MonoBehaviour {
public Transform prefab;
private LinkedList<Transform> roads = new LinkedList<Transform>();
private float posY = 0.0f;
private int numberOfRoads = 5;
// Use this for initialization
void Start () {
// Init the scene with some road-pieces
for(int i=0;i < numberOfRoads;i++) {
Transform road = Instantiate(prefab) as Transform;
road.Translate(0, posY, i * road.localScale.z);
roads.AddLast(road);
}
}
// Update is called once per frame
void Update ( ) {
Transform firstRoad = roads.First.Value;
Transform lastRoad = roads.Last.Value;
// Create a new road if the first one is not
// in sight anymore and destroy the first one
if(firstRoad.localPosition.z < -40f) {
roads.Remove(firstRoad);
Destroy(firstRoad.gameObject);
Transform newRoad = Instantiate(prefab, new Vector3(0, posY ,
lastRoad.localPosition.z + lastRoad.localScale.z),
Quaternion.identity) as Transform;
roads.AddLast(newRoad);
}
// Move the available roads along the z-axis
foreach(Transform road in roads) {
road.Translate(0,0, -10f * Time.deltaTime);
}
}
//////////Collision Here////////////
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "Hardle") {
print("Collision-moveRoad");
animation.CrossFade("jump_pose", 0.02F);
//animation.Play("idle");
// Time.timeScale= 0.0f;
}
}
}
Have a boolean variable called moving
. Only do the foreach
when moving
is true. Set moving
false in your collision code.
Answer by FrankHHansen · Jan 23, 2014 at 12:00 PM
I would sugget making a Bool that you could call moveRoads and set that to true, Upon collision, set it to false.
in the update where the roads are moved, check if moveRoads is true and move the roads if it is.
// Move the available roads along the z-axis
foreach(Transform road in roads)
{
//Only move the roads if the moveRoads is true.
if(moveRoads)
{
road.Translate(0,0, -10f * Time.deltaTime);
}
}
@frank Ok ... now on collision my road stop moving but when i collide with the object (Hardle) my player push the hurdle and through it away .. .... what i should do next ... on my object rigid body is applied, box collider is applied, in rigid body either gravity toogle on or off same stuff....... i applied rigid body on the player as well , his mass 1 and Hardle mass 100 but again player through it away ... :/ ...
Your answer
Follow this Question
Related Questions
Simple collision? 2 Answers
OnCollisionEnter2D not being called on entering new tile once already called. 1 Answer
Distribute terrain in zones 3 Answers
Checking for collision with 2D objects/sprites 3 Answers
Collision issue with SetParent (C#) 1 Answer