Coin magnet like effect, transform.translate help needed

Hello, i want the magnet effect on my coins, for that i want to move it so that it moves towards my hero, intersect him and go forward…

see this image >> Imgur: The magic of the Internet

I need movement across z[guy is running in z +ve direction] and x[left right] and this is the script attached to the coin , seee in void start() and guide me the right movement…

using UnityEngine;
using System.Collections;

public class bottleCollector : MonoBehaviour {
	
	public static int _currentBottlesCollected=0;
	public int magnetDistance = 10; 
	

	// Use this for initialization
	void Start () {
		//do -90 to make it straight
	transform.Rotate(-90, 0, 0);
		float playerpos_z= characterScript.hero_z_position;
		float bottlepos_z= transform.position.z;
		float disdiff_z = bottlepos_z - playerpos_z;
		
		float playerpos_x= characterScript.hero_x_position;
		float bottlepos_x= transform.position.x;
		float disdiff_x = bottlepos_x - playerpos_x;
		
		//transform.Translate(disdiff_x,transform.position.y,disdiff_z);
		transform.Translate(Vector3.forward * Time.deltaTime);
		
		//Debug.Log("Player pos z="+playerpos_z+",Bottle pos z="+bottlepos_z+",diff z="+disdiff_z);
		//Debug.Log("Player pos x="+playerpos_x+",Bottle pos x="+bottlepos_x+",diff z="+disdiff_x);
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		float a;
		float b;
		a = characterScript.hero_z_position;
		b = transform.position.z;
		//Debug.Log(a);
	//transform.Translate(-Vector3.forward * Time.deltaTime);
		if(characterScript.hero_z_position>transform.position.z){
			//Debug.Log("bottle is at the back");
			Destroy(gameObject);
		}
	}
	
	
	void Update(){
		
		
		//Vector3 bottlepos = transform.position;
		//Debug.Log(playerpos);
		
//		  if(Vector3.Distance(playerpos, transform.position) < magnetdistance){
//   transform.position = Vector3.Lerp(transform.position, player.transform.position, Time.deltaTime);
//  }
	}

	void OnTriggerEnter(Collider other) {
		if(other.gameObject.tag == "Player"){
			_currentBottlesCollected=_currentBottlesCollected+1;
			scoreHandler.currentgameBottlesCollected = _currentBottlesCollected;
		//	Debug.Log(scoreHandler.currentgameBottlesCollected);
		//	Debug.Log(".."+_currentBottlesCollected);
			Destroy(gameObject);
    }}
	
	 void OnBecameInvisible() {
		//Debug.Log("Invisible bottle");
        Destroy(gameObject);
    }
}

But if i try to add this line of code in my FixedUpdate then i get these errors-

transform.position = Vector3.Lerp(transform.position, characterScript.transform.position, Time.deltaTime);
  1. Assets/scripts/bottleCollector.cs(32,87): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.transform’
  2. Assets/scripts/bottleCollector.cs(32,46): error CS1502: The best overloaded method match for `UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)’ has some invalid arguments
  3. Assets/scripts/bottleCollector.cs(32,46): error CS1503: Argument #2' cannot convert object’ expression to type `UnityEngine.Vector3’

This is my characterScript incase you wish to see- http://pastebin.com/Mi1taSYa

Hi, it strikes me as characterScript doesn’t inherited from MonoBehaviour class.

Try to write this:

transform.position = Vector3.Lerp(transform.position, characterScript.pos, Time.deltaTime);

UPD.: Sorry, I didn’t see characterScript code before:hushed:. It’s realy inherited from MonoBehaviour. In any case try to rewrite code as in my example (I correct it a little bit). :wink:

using UnityEngine;
using System.Collections;
 
public class bottleCollector : MonoBehaviour {
       
        public static int _currentBottlesCollected=0;
        public int magnetDistance = 10;
        public GameObject soldier;
 
        // Use this for initialization
        void Start () {
                soldier = GameObject.Find("Soldier");
                //do -90 to make it straight
        transform.Rotate(-90, 0, 0);
                float playerpos_z= characterScript.hero_z_position;
                float bottlepos_z= transform.position.z;
                float disdiff_z = bottlepos_z - playerpos_z;
               
                float playerpos_x= characterScript.hero_x_position;
                float bottlepos_x= transform.position.x;
                float disdiff_x = bottlepos_x - playerpos_x;
               
                //transform.Translate(disdiff_x,transform.position.y,disdiff_z);
                transform.Translate(Vector3.forward * Time.deltaTime);
               
                //Debug.Log("Player pos z="+playerpos_z+",Bottle pos z="+bottlepos_z+",diff z="+disdiff_z);
                //Debug.Log("Player pos x="+playerpos_x+",Bottle pos x="+bottlepos_x+",diff z="+disdiff_x);
        }
       
        // Update is called once per frame
        void FixedUpdate () {  
               
               
                //transform.position = Vector3.Lerp(transform.position, soldier.transform.position, Time.deltaTime);
               
                float a;
                float b;
                a = characterScript.hero_z_position;
                b = transform.position.z;
                //Debug.Log(a);
        //transform.Translate(-Vector3.forward * Time.deltaTime);
                if(characterScript.hero_z_position>transform.position.z){
                        //Debug.Log("bottle is at the back");
                        Destroy(gameObject);
                }
        }
       
       
        void Update(){
               
               
                //Vector3 bottlepos = transform.position;
                //Debug.Log(playerpos);
               
//                if(Vector3.Distance(playerpos, transform.position) < magnetdistance){
//   transform.position = Vector3.Lerp(transform.position, player.transform.position, Time.deltaTime);
//  }
        }
 
        void OnTriggerEnter(Collider other) {
                if(other.gameObject.tag == "Player"){
                        _currentBottlesCollected=_currentBottlesCollected+1;
                        scoreHandler.currentgameBottlesCollected = _currentBottlesCollected;
                //      Debug.Log(scoreHandler.currentgameBottlesCollected);
                //      Debug.Log(".."+_currentBottlesCollected);
                        Destroy(gameObject);
    }}
       
         void OnBecameInvisible() {
                //Debug.Log("Invisible bottle");
        Destroy(gameObject);
    }
}