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

Copy collision object position

Discussion in 'Scripting' started by Khyze, Jul 15, 2016.

  1. Khyze

    Khyze

    Joined:
    Jul 11, 2016
    Posts:
    3
    So.... there has been like 4 days and i still cant pass through this.... I made a custom Gravity system (Which is pretty cool and kinda works fine), but the problem is that im a newbie here, I dont know the exact codes that i need, here is an image of the Problem:



    The object stops from failling correctly and all jumping values return to zero so it can jump again, the problem is i dont know a way to stay on top of the platform, by logic i know why is this problem, but i dont know the codes to fill everything...

    Lets say, speed is 5, the platform distance is 25-45 (First number is the beginning and 45 is the ending, is like the size)

    If we keep 5+ eventually we will get 25, 5+5+5+5+5=25, so it will perfectly land, but what will happen if we change speed to 10? Well, it will not work exactly as good because: 10+10+10=30, is the closest number, so the top was 25, we will have a distance of 5 inside the platform... And well, the numbers will change eventually (Falling on the same speed is kinda dummy), and maybe with dots like 2.53

    Here is my code:
    Code (csharp):
    1.  
    2. voidOnCollisionStay(Collision col)
    3. {
    4. if(col.gameObject.tag =="StageCol")
    5. {
    6. if(jumpSpeed<0)
    7. {canjump=true;
    8. jumping=false;
    9. jumpSpeed=0;
    10. //transform.position+=new Vector3(0f,0.1f,0f);
    11. //transform.position.y = col.transform.position.y;
    12. }}
    13. }
    I tryed so many things, but i still dont understand how Unity works with the x,y,z planes.... i tryed everythin that came on my head about transform and position, always with an error...

    I guess there are two solutions, the first one is copy the y plane of the collision object (Platform), in the previous example it will return 25, but i dont know how, the other solution is to use loops:

    https://unity3d.com/es/learn/tutorials/topics/scripting/loops

    It seems a little bit easyer, but i still dont understand how are the values to increase the y on a point where it not collide with the floor...

    Also, like 3 Days ago i writed this:
    http://answers.unity3d.com/questions/1214678/copy-collision-object-position-or-just-fly-a-littl.html
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  3. Deleted User

    Deleted User

    Guest

    I don't understand why you want to do it like that. Why you don't just use physics to collide with platform? It can do everything for you. You are using OnCollisionStay, so you are using rigidbody and colliders. And why you need custom gravity?

    Instead of changing transform position of object, you should use AddForce to rigidbody.
    https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

    or set position using rigidbody
    https://docs.unity3d.com/ScriptReference/Rigidbody-position.html

    If you really need to move it using transform then use Physics.Raycast in every step to get if object will collide and where. Just send ray from bottom of object to vector where you want to move it. If raycast find something then you can move it to that position (hit.point).
    https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
     
  4. Khyze

    Khyze

    Joined:
    Jul 11, 2016
    Posts:
    3
    It makes a difference? It seems like the same to me :p (Also, im newbie :3)

    Well, i tryed doing the jumps with Rigidbody, but i read a lot of coments (From answer.unity) which says that for Cartoon character (Or unreal games) isnt very usefull (Since there is no need to have realistic Weights or Gravity)

    I used Rigidbody because i didnt find another way to do collisions, but im not really using it for something else:


    Just for collisions, altough this seems nice:
    https://docs.unity3d.com/ScriptReference/Rigidbody-position.html

    It says it faster....

    Code (csharp):
    1. if (jumping)
    2. {//transform.position+= new Vector3(0f, jumpSpeed*Time.deltaTime, 0f);
    3. rb.AddForce(0f, jumpSpeed*Time.deltaTime, 0f, ForceMode.Impulse);
    4. if (jumpSpeed>=-20) jumpSpeed-=1;
    5. }
    Still i dont have any idea how it works, so.... Is imposible to get other y value?

    It seems easyer.... well i will try that Raycast, but i feel what i want is easyer than all of that .-.

    Thx for answering o.o, why answer.unity took so long? is inactive?