Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Coin magnet like effect, transform.translate help needed

Discussion in 'Scripting' started by jessica1986, May 27, 2013.

  1. jessica1986

    jessica1986

    Joined:
    Feb 7, 2012
    Posts:
    621
    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 >> http://imgur.com/ydzS1U2
    $ydzS1U2.jpg


    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..

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class bottleCollector : MonoBehaviour {
    5.    
    6.     public static int _currentBottlesCollected=0;
    7.     public int magnetDistance = 10;
    8.    
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         //do -90 to make it straight
    13.     transform.Rotate(-90, 0, 0);
    14.         float playerpos_z= characterScript.hero_z_position;
    15.         float bottlepos_z= transform.position.z;
    16.         float disdiff_z = bottlepos_z - playerpos_z;
    17.        
    18.         float playerpos_x= characterScript.hero_x_position;
    19.         float bottlepos_x= transform.position.x;
    20.         float disdiff_x = bottlepos_x - playerpos_x;
    21.        
    22.         //transform.Translate(disdiff_x,transform.position.y,disdiff_z);
    23.         transform.Translate(Vector3.forward * Time.deltaTime);
    24.        
    25.         //Debug.Log("Player pos z="+playerpos_z+",Bottle pos z="+bottlepos_z+",diff z="+disdiff_z);
    26.         //Debug.Log("Player pos x="+playerpos_x+",Bottle pos x="+bottlepos_x+",diff z="+disdiff_x);
    27.     }
    28.    
    29.     // Update is called once per frame
    30.     void FixedUpdate () {
    31.         float a;
    32.         float b;
    33.         a = characterScript.hero_z_position;
    34.         b = transform.position.z;
    35.         //Debug.Log(a);
    36.     //transform.Translate(-Vector3.forward * Time.deltaTime);
    37.         if(characterScript.hero_z_position>transform.position.z){
    38.             //Debug.Log("bottle is at the back");
    39.             Destroy(gameObject);
    40.         }
    41.     }
    42.    
    43.    
    44.     void Update(){
    45.        
    46.        
    47.         //Vector3 bottlepos = transform.position;
    48.         //Debug.Log(playerpos);
    49.        
    50. //        if(Vector3.Distance(playerpos, transform.position) < magnetdistance){
    51. //   transform.position = Vector3.Lerp(transform.position, player.transform.position, Time.deltaTime);
    52. //  }
    53.     }
    54.  
    55.     void OnTriggerEnter(Collider other) {
    56.         if(other.gameObject.tag == "Player"){
    57.             _currentBottlesCollected=_currentBottlesCollected+1;
    58.             scoreHandler.currentgameBottlesCollected = _currentBottlesCollected;
    59.         //  Debug.Log(scoreHandler.currentgameBottlesCollected);
    60.         //  Debug.Log(".."+_currentBottlesCollected);
    61.             Destroy(gameObject);
    62.     }}
    63.    
    64.      void OnBecameInvisible() {
    65.         //Debug.Log("Invisible bottle");
    66.         Destroy(gameObject);
    67.     }
    68. }
    69.  

    But if i try to add this line of code in my FixedUpdate then i get these errors-
    Code (csharp):
    1. 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
     
    Last edited: May 27, 2013
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Hi, it strikes me as characterScript doesn't inherited from MonoBehaviour class.

    Try to write this:
    Code (csharp):
    1. transform.position = Vector3.Lerp(transform.position, characterScript.pos, Time.deltaTime);
    UPD.: Sorry, I didn't see characterScript code before:eek:. It's realy inherited from MonoBehaviour. In any case try to rewrite code as in my example (I correct it a little bit). ;)
     
    Last edited: May 27, 2013
  3. Bladesfist

    Bladesfist

    Joined:
    Jan 3, 2012
    Posts:
    107
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class bottleCollector : MonoBehaviour {
    5.        
    6.         public static int _currentBottlesCollected=0;
    7.         public int magnetDistance = 10;
    8.         public GameObject soldier;
    9.  
    10.         // Use this for initialization
    11.         void Start () {
    12.                 soldier = GameObject.Find("Soldier");
    13.                 //do -90 to make it straight
    14.         transform.Rotate(-90, 0, 0);
    15.                 float playerpos_z= characterScript.hero_z_position;
    16.                 float bottlepos_z= transform.position.z;
    17.                 float disdiff_z = bottlepos_z - playerpos_z;
    18.                
    19.                 float playerpos_x= characterScript.hero_x_position;
    20.                 float bottlepos_x= transform.position.x;
    21.                 float disdiff_x = bottlepos_x - playerpos_x;
    22.                
    23.                 //transform.Translate(disdiff_x,transform.position.y,disdiff_z);
    24.                 transform.Translate(Vector3.forward * Time.deltaTime);
    25.                
    26.                 //Debug.Log("Player pos z="+playerpos_z+",Bottle pos z="+bottlepos_z+",diff z="+disdiff_z);
    27.                 //Debug.Log("Player pos x="+playerpos_x+",Bottle pos x="+bottlepos_x+",diff z="+disdiff_x);
    28.         }
    29.        
    30.         // Update is called once per frame
    31.         void FixedUpdate () {  
    32.                
    33.                
    34.                 //transform.position = Vector3.Lerp(transform.position, soldier.transform.position, Time.deltaTime);
    35.                
    36.                 float a;
    37.                 float b;
    38.                 a = characterScript.hero_z_position;
    39.                 b = transform.position.z;
    40.                 //Debug.Log(a);
    41.         //transform.Translate(-Vector3.forward * Time.deltaTime);
    42.                 if(characterScript.hero_z_position>transform.position.z){
    43.                         //Debug.Log("bottle is at the back");
    44.                         Destroy(gameObject);
    45.                 }
    46.         }
    47.        
    48.        
    49.         void Update(){
    50.                
    51.                
    52.                 //Vector3 bottlepos = transform.position;
    53.                 //Debug.Log(playerpos);
    54.                
    55. //                if(Vector3.Distance(playerpos, transform.position) < magnetdistance){
    56. //   transform.position = Vector3.Lerp(transform.position, player.transform.position, Time.deltaTime);
    57. //  }
    58.         }
    59.  
    60.         void OnTriggerEnter(Collider other) {
    61.                 if(other.gameObject.tag == "Player"){
    62.                         _currentBottlesCollected=_currentBottlesCollected+1;
    63.                         scoreHandler.currentgameBottlesCollected = _currentBottlesCollected;
    64.                 //      Debug.Log(scoreHandler.currentgameBottlesCollected);
    65.                 //      Debug.Log(".."+_currentBottlesCollected);
    66.                         Destroy(gameObject);
    67.     }}
    68.        
    69.          void OnBecameInvisible() {
    70.                 //Debug.Log("Invisible bottle");
    71.         Destroy(gameObject);
    72.     }
    73. }