Search Unity

Noob needing help coding

Discussion in 'Scripting' started by A.A.G., Dec 3, 2012.

  1. A.A.G.

    A.A.G.

    Joined:
    Nov 28, 2012
    Posts:
    7
    Hi im working in my own game alone with no help, i have been watching tons of tutorials and reading more, but english is not my mother lenguage so i cant seem to repoduce or to fully understand whats going on...

    so for example if i want to create in unity a game object with is own gravity (Atract "projectile" but not the player)

    var gameObject= gravity;
    public var gameObjectGravity= 10

    and if i want to "shoot" a game object that will be atracted to the gravity of that game object and when i press "E" botton the fuction of destroying will be set on or off. ( i took this code from another FPS as a basic to modify )

    function start(){
    if( Input.GetButtonDown( "e" ) )

    var asExplosion : boolean = false;


    var explosion : GameObject;
    function (OnCollisionEnter collision : Collision );
    var contact : ContactPoint = collision.contacts[0];
    var rotation = Quaternion.FromToRotation( Vector3.up, contact.normal );
    var instantiatedExplosion : GameObject = Instantiate(
    explosion, contact.point, rotation );
    Destroy( gameObject );
    }
     
    Last edited: Dec 3, 2012
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    well...
    1. none of that code does any gravity type pull?
    2. you cant put functions inside functions (You have OnCollisionEnter inside Start, and its not even defined correctly anyway)
    3. you should avoid using names that are similar/close to standard types/objects (ie gameObject, transform)

    I only code in c#, but ill give some js a go here...

    Code (csharp):
    1.  
    2.  
    3. var GravitySource : GameObject;
    4. var GravityPull : float = 10f;
    5. var GravityEnabled : bool = false;
    6.  
    7. function Update()
    8. {
    9.   GravityEnabled = !Input.GetKeyDown(KeyCode.E);
    10.   if(GravityEnabled  GravitySource != null)
    11.   {
    12.     transform.position = Vector3.MoveTowards(transform.position, GravitySource.position, GravityPull * Time.deltaTime);  
    13.   }
    14. }
    15.  
    16.  
    17. var explosion : GameObject;
    18. function OnCollisionEnter (collision : Collision )
    19. {
    20.   if(explosion != null)
    21.   {
    22.     var contact : ContactPoint = collision.contacts[0];
    23.     var rotation = Quaternion.FromToRotation( Vector3.up, contact.normal );
    24.     var instantiatedExplosion : GameObject = Instantiate(explosion, contact.point, rotation );
    25.   }
    26.   Destroy( gameObject );
    27. }
    28.  
    29.  
    30.  
     
  3. A.A.G.

    A.A.G.

    Joined:
    Nov 28, 2012
    Posts:
    7
    thanks JamesLeeNZ your awesome for helping me out, now i cant try out the code because it says that this script is wrong or obsolete ???? something that haves going on with unity since update to 4.x...
    using UnityEngine;

    public class ActivateTrigger : MonoBehaviour {
    public enum Mode {
    Trigger = 0, // Just broadcast the action on to the target
    Replace = 1, // replace target with source
    Activate = 2, // Activate the target GameObject
    Enable = 3, // Enable a component
    Animate = 4, // Start animation on target
    Deactivate= 5 // Decativate target GameObject
    }

    /// The action to accomplish
    public Mode action = Mode.Activate;

    /// The game object to affect. If none, the trigger work on this game object
    public Object target;
    public GameObject source;
    public int triggerCount = 1;///
    public bool repeatTrigger = false;

    void DoActivateTrigger () {
    triggerCount--;

    if (triggerCount == 0 || repeatTrigger) {
    Object currentTarget = target != null ? target : gameObject;
    Behaviour targetBehaviour = currentTarget as Behaviour;
    GameObject targetGameObject = currentTarget as GameObject;
    if (targetBehaviour != null)
    targetGameObject = targetBehaviour.gameObject;

    switch (action) {
    case Mode.Trigger:
    targetGameObject.BroadcastMessage ("DoActivateTrigger");
    break;
    case Mode.Replace:
    if (source != null) {
    Object.Instantiate (source, targetGameObject.transform.position, targetGameObject.transform.rotation);
    DestroyObject (targetGameObject);
    }
    break;
    case Mode.Activate:
    GameObject = true;
    break;
    case Mode.Enable:
    if (targetBehaviour != null)
    targetBehaviour.enabled = true;
    break;
    case Mode.Animate:
    GameObject.animation.Play ();
    break;
    case Mode.Deactivate:
    GameObject.active = false;
    break;
    }
    }
    }

    void OnTriggerEnter (Collider other) {
    DoActivateTrigger ();
    }
    }
     
  4. BlankFoxGirl

    BlankFoxGirl

    Joined:
    Apr 26, 2010
    Posts:
    71
    Just some general housekeeping to help us help you...

    In future, could you please wrap your code in the Code, or PHP tags? Also, when you are stating there is an error, please copy and paste the full error and identify which line of code it is applicable to?
    PHP:
    Function Start(){

    }
    Code (csharp):
    1. Function Start(){
    2.  
    3. }
    Judging by the sounds of your error, a function used by one or more of your lines of code is no longer supported or will be removed in an upcoming version of the Unity Editor. :)
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
  6. A.A.G.

    A.A.G.

    Joined:
    Nov 28, 2012
    Posts:
    7
    im sorry guys for the inconvenience, ill try doing my best now on.

    Thanks again guys ur the best... all coding error fixed

    now lets try that code JamesLeeNZ help me out with.