Temple run like collect coins

I’m trying to make a car that is moving at various speeds and when it collide with a magnet and if it gets close enough to an array of coins it will suck it in towards the car…If you don’t understand think of Temple Run and the magnet power-up and how it sucks in the coins. Something like that…M looking for any tutorial or script like this or any other help from u guys…:slight_smile:
Thnx…

Apply a trigger to the coins and the car.
Also apply a script to the car which manages the coin and magnet pickup.
If a magnet power-up is used increase the size of the cars trigger.

But how it can attract an arry of coins??

@Jawad try these scripts…

Steps…
1.Create a car script.
2.Create a coin script.

In your car script, add these lines

#pragma strict
static var attractCoins: boolean = false;
function Start () {

}

function Update () {

}

function OnCollisionEnter(col : Collision)
{
    if(col.collider.tag == "Magnet")
    {
        attractCoins= true;
    }
}

In your coin script, add these lines

#pragma strict
var target : GameObject;

function Start () {

    target = GameObject.FindGameObjectWithTag("Car");
}

function Update () {

    if(car.attractCoins== true  && Vector3.Distance(transform.position,target.transform.position) < 5.0)
    {
        transform.position = Vector3.MoveTowards(transform.position, target.transform.position, Time.deltaTime*5);
    }
}
  1. Now attach the car script to your Car and attach the coin script to your Coins.
  2. as per these scripts , your Magnet should be tag as “Magnet” and your Car should be tag as “Car”

car script will set attractCoins = true , when your car collides with Magnet.
And your coin script check if car.attractCoins == true and distance between your Car and Coin is less than 5, than it MoveTowards your car.

Hi Sushanta…Thnx for ur time…:slight_smile: In coin script line # 11 car stands for gameobject or what??? i got error on this… Unkown identifier…:frowning:

Replace “car” with the name of your car script (the first one sushanta posted). Because attractCoins is static you don’t need a direct reference.

Done it…But not working Dear…:frowning:

Actually i have applied 2 scripts on car…
Magnet

var hit : boolean = false;

function Start () {
 
}

function Update () {
	this.transform.Translate(Vector3.forward * Time.deltaTime);
	if(hit){
	gameObject.GetComponent( Test1 ).enabled = true;
	hit = false;
	print("sssss");

}
	
}
function OnTriggerEnter(col : Collider){
	if(col.transform.tag == "Magnet"  hit == false ){
	
	print("aaaa");
	Destroy (col.gameObject);
	hit = true;
	}

}

Car

var target : GameObject[];
var i;
var pullSpeedFactor : float = 1.0;
private var hit : boolean = false;

function Update () {
		
 	//	var diff : Vector3 = this.transform.position - targetObject.transform.position;
 	///	targetObject.transform.position = rigidbody.AddForce (0, 10, 0);
 	//	hit = true;
 		

}
function FixedUpdate () {
	if(target.Length > 0  target.Length != null  hit== false){
		for(var i=0;i<target.length;i++){
		var diff : Vector3 = this.transform.position - target[i].transform.position;
    	target[i].rigidbody.AddForce((diff * 2)/5);
    	hit = false;
    	
		}
	
	
	}
   
    
}
function OnCollisionStay(collision : Collision) {
    // Check if the collider we hit has a rigidbody
    // Then apply the force
   if (collision.rigidbody) {
    	//collision.rigidbody.AddForce(0,0,0);
     for(var i=0;i<target.length;i++){
		Destroy (target[i]);
		hit = true;
			}
	  }
   }

Both working fine…but i want the coins which are not in the car’s way also attract towards car by rotating themselves towards the car and when i ll not get magnet then coins will collect in a normal manner…

Hi jawad…
You are doing something wrong, otherwise those scripts should work, They are in the most simplest from that i can imagine :slight_smile:
here is a working example, using those 2 scripts i showed you.

http://dl.dropbox.com/u/42396722/car/car.html

White Box is the Car, blue one is the Magnet, and yellow ones are the Coins.

Ya this thing i want which u showed me…I think i should try once again…Thnx alot yr…:slight_smile:

Coin script applied on each coin???

yes

Thnx again Sushanta…:slight_smile: its working…:slight_smile:

dude can u help me, i make simple game without a car, just a thirhperson player, and i want to make the same thing, collect coins… i tried that u gave but wont work =/