Search Unity

Help for creating a magnet-like effect.

Discussion in 'Scripting' started by legmog, May 15, 2008.

  1. legmog

    legmog

    Joined:
    May 8, 2008
    Posts:
    21
    Hey people. First of all just to say that I am a newbie at Unity. I have an extremely basic degree of the Java scripting (I have only been practicing for a week)...

    I have already made some other threads and I give great thanks to those who have helped me out... But I have a question for you.....

    Here's what I want to do....

    I am making a game, and I want it so when the character approaches an object (in this case energy ball items), the objects must get attracted to the character and move towards him.
    Think of it like a magnet. With the character being the magnet, and the object being the piece of metal.
    When the character approaches the object... The object should be pulled towards the character.

    This type of thing is employed in a number of games, namely when you collect items on the ground.... For example the latest Ratchet and Clank game uses a similar system. This is what I want do try and achieve.

    I presume this would be somewhat simple to achieve... It's a case of when the character gets within a certain distance of said object, the object gets attracted to the character, like a gravitational pull or something... Said object then follows the character around wherever he goes because it is always attracted to it.


    Now perhaps I should explain a bit about the game I am making so this makes more sense......

    The aim of the game will be to go around the environment getting all these objects (energy balls). You just get a load to follow you.... then you have to go and drop them back at base.

    So as for the code...
    The character should have a code on him which somehow attracts the energy objects to get drawn to him.
    (I would also preferably want it so the object will stay following the character).

    The Base must also have a code on it which has a greater pull than that of the character... so when you go back to base, the energy objects are drawn away from the character and towards to base.


    If anyone could give me any tips or codes on how to achieve this then It would be GREATLY appreciated.

    Thank you for taking the itme to read this rather long post.
     
  2. legmog

    legmog

    Joined:
    May 8, 2008
    Posts:
    21
    Ok I did a search on this forum and found a similar kind of thing...... Though it's not quite the same.

    This is the code I found



    Basically this code makes it so when I push '1' on the keyboard.... All rigid body objects get attracted to the character. However after a little while, the rigid bodies lose their attraction and simply fly away...

    If anyone has any ideas of how I could modify this code to gain an effect of how I described in my first post, then I would appreciate it.
     
  3. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    Hey, that looks familiar!


    If I recall, the mass for the rigidbodies has to be set right for that script to work very well.

    For something like you are describing, I might tag each energy sphere "Energy" and put something like this on the player's GameObject:


    Code (csharp):
    1. function FixedUpdate () {
    2.     var energySpheres = GameObject.FindGameObjectsWithTag("Energy");
    3.     for (var energySphere in energySpheres){
    4.         var vsub = transform.position - energySphere.GetComponent(Transform).position;
    5.         energySphere.GetComponent(Rigidbody).AddForce(vsub.normalized * (Mathf.Max((10 - vsub.magnitude)/5, 0)), ForceMode.VelocityChange);
    6.     }
    7. }
    You probably want to change the way that they behave once they have been sucked in, but basically we are finding the direction and distance between them and if they are close, pulling them towards the player.
     
  4. legmog

    legmog

    Joined:
    May 8, 2008
    Posts:
    21
    Hey. Thanks for the response.. (And yes that was the code you wrote for the other guy ^^)
    Though I was able to find a different code from the Unity Scripting files. I searched explosion and found a code which applies an explosive force to the object that it is applied to.
    So I applied it to the main character, but I instead set the numeric explosive variables for 'power' to MINUS... Thus instead of objects being pushed away when I approached them..... They were attracted to me.

    After a little tinkering around with the settings, I was able to get it so the objects could never get so far away from my character that they would exit the gravitational pull and fly off into infinite.

    This was the code I ended up using...



    This really is exactly what I wanted...... However there is one thing..... At the moment this code applies to EVERY single rigid body in the game.... However I only want it to apply to the Energy Balls....

    What code do I need to insert into this, so that only the energy balls will be attracted by the gravitational pull?
    Once I know this I will be well on my way to finishing my game :D. And thank you very much again for your help ^^.
     
  5. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    You could tag your energy spheres as something like "Energy" and then where you are checking if the object is a rigidbody, also check to make sure it has that tag.
     
  6. legmog

    legmog

    Joined:
    May 8, 2008
    Posts:
    21
    Hey, thanks for the REALLY quick response ^^....
    Though I am not sure how I would do what you just said.. Where in the code I posted above would I insert what you said and how would I write it?

    Please forgive my newbishness as I'm completely new to both Untiy and Java scripting, with zero experience in either of them...
     
  7. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    Just change:


    Code (csharp):
    1.  
    2. if (hit.rigidbody)
    3.  
    to

    Code (csharp):
    1.  
    2. if (hit.rigidbody  hit.tag == "Energy")
    3.  
    Assuming that you have tagged your energy spheres with a tag named "Energy".
     
  8. legmog

    legmog

    Joined:
    May 8, 2008
    Posts:
    21
    aaah that works perfectly! Thanks a lot you have REALLY helped me out ^^.....
    I guess I really should start trying to learn Java script a bit more ^^..
     
  9. hepsijaci

    hepsijaci

    Joined:
    Sep 7, 2017
    Posts:
    1
    can anyone tel me,,how to i collect the coin without getting stopped by Obstacles,.
    I fixed my code if the player hit the obstacles the speed should be zero.
    but my player consider the coin also the part of obstacles......
    so if they hit the coin or obstacles they get stopped.......


    here i attached my Coding......


    using System.Collections;

    using UnityEngine;

    public class Player : MonoBehaviour {
    public float speed = 5f;
    public float jumpingForce=450f;
    public float jumpingCooldown=0.6f;
    private float jumpingtimer = 0f;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    transform.position +=speed * Vector3.forward * Time.deltaTime;
    jumpingtimer -= Time.deltaTime;
    if (GvrViewer.Instance.Triggered || Input.GetKeyDown ("space")) {
    if (jumpingtimer <= 0f) {
    jumpingtimer = jumpingCooldown;
    this.GetComponent<Rigidbody>().AddForce(Vector3.up*jumpingForce);

    }
    }
    }
    void OnTriggerEnter(Collider collider)
    {
    if(collider.tag == "Coin");
    {
    speed *= 1f;
    }
    if(collider.tag == "Obstacles");
    {
    speed *= 0f;
    }
    }
    }