Search Unity

Player Pushes Enemies, and I'm out of ideas

Discussion in 'Editor & General Support' started by CG-DJ, Sep 7, 2013.

  1. CG-DJ

    CG-DJ

    Joined:
    Jan 28, 2012
    Posts:
    73
    Ok, I've been working at this bug for 3 days now!

    Here is a link to a Unity Answers question I started. I am now making a forum page for it because I feel this problem needs more discussion.

    I'm making a 2D platformer. I have my main character using a character controller for it's movement, and my enemies using a rigidbody and a MovePosition function. The problem is, when the player runs into the enemy, the enemy bounces backwards and is pushed back. I don't want that. I want to be able to run into the enemy and the enemy doesn't move! Now, after A LOT of digging, I found that when I set my velocityX variable to 0 (making the MovePosition stop), then it acts like a collider and won't move when I run into it. And, it only gets pushed when I run into from the direction it is traveling. If I run into it from behind, then it won't be pushed. But when there is any movement of the enemy, then it gets pushed around. Any suggestions?

    Links to my scripts: EnemyScript - MainCharScript

    UPDATE (also amended to the first post):

    I'm back again... I totally rewrote the script. I took everything to the dry erase board and wrote it all out before I typed it up. The pushing problem still persists though. I can't figure out what's going wrong!!! Any ideas? (If you want, my skype is cg.artist4jc. If you contact request me, please put "Unity" in the contact request message.)

    |------------- LINK TO SCRIPT -------------|
     
    Last edited: Sep 18, 2013
  2. Xian55

    Xian55

    Joined:
    Jul 6, 2012
    Posts:
    12
  3. CG-DJ

    CG-DJ

    Joined:
    Jan 28, 2012
    Posts:
    73
    Yeah, the problem isn't ignoring collision, it's that the player is pushing the enemy, when it should really just run into the enemy and stop. I want the player to run into the enemy and stop (thus, there needs to be collision). What I need to figure out is why the MovePosition function (if that is even the problem) is making so that the character controller affects velocity.
     
  4. jvil

    jvil

    Joined:
    Jul 13, 2012
    Posts:
    263
    MovePosition will teleport an object to desired position, which usually is a bad idea if you plan to move enemies around a scenario with other colliders like walls or other colliders, because will ignore physics (also on irregular ground can make the enemy fall off).

    Try using rigidbody.AddForce() for moving enemies.
     
  5. CG-DJ

    CG-DJ

    Joined:
    Jan 28, 2012
    Posts:
    73
    I'll try it :)
     
  6. CG-DJ

    CG-DJ

    Joined:
    Jan 28, 2012
    Posts:
    73
    Ok, I tried changing it to AddForce. Problem still exists! I just made a video showing my problem. If you can't see it, let me know!

    Link to Video
     
    Last edited: Sep 7, 2013
  7. Swearsoft

    Swearsoft

    Joined:
    Mar 19, 2009
    Posts:
    1,632
    comment this out.
     
  8. CG-DJ

    CG-DJ

    Joined:
    Jan 28, 2012
    Posts:
    73
    what? Are you saying I need more comments in my code?
     
  9. Swearsoft

    Swearsoft

    Joined:
    Mar 19, 2009
    Posts:
    1,632
    No

    use /* at the beginning and */ at the end, this will turn the whole block into a comment, effectively removing it, but without you deleting the code.
    Save and test.
     
  10. CG-DJ

    CG-DJ

    Joined:
    Jan 28, 2012
    Posts:
    73
    Ok tried that. I commented out that trigger part, but the problem kept occuring. Then I tried commenting out the OnCollisionEnter to the player, and it kept happening. Then I tried commenting out the entire OnCollisionEnter....and it kept happening!

    Ok, does this normally happen? or is this a fluke?
     
  11. Swearsoft

    Swearsoft

    Joined:
    Mar 19, 2009
    Posts:
    1,632
    I'm too bored to replicate you rproject, if you send me the a scene with the required objects I can check it out.
     
  12. CG-DJ

    CG-DJ

    Joined:
    Jan 28, 2012
    Posts:
    73
    Ok, here is a link to the entire project:
    https://dl.dropboxusercontent.com/u/33872519/Pushing Enemies/PushEnemyProblem.zip

    I'm currently in Scene "Testing LongRun", and the enemy I am testing against is called "Cube-TestPush". Please note that when I zipped the project, I still had the OnCollisonEnter commented out, so you may want to fix that.

    I also have commented out everything to change the enemy's state. So that means it will always be in idle mode, no matter what.

    Thanks for looking at this, I hope you have better ideas than I have!
     
  13. Swearsoft

    Swearsoft

    Joined:
    Mar 19, 2009
    Posts:
    1,632
    You are controlling too much in too many places. You have to streamline the decision making process in your ai (enemy script).

    I would start over, it's simply too entangled, without priority or a clear design. This is not a Unity bug, you are simply overwriting, changing and altering how the cube should work based on too many checks in too many places. You either have to add debugs to almost every line in order to understand what the spaghetti is doing or start over in a more methodical manner.

    Sorry.

    K.
     
  14. CG-DJ

    CG-DJ

    Joined:
    Jan 28, 2012
    Posts:
    73
    Ok. This is my first real game, and I'm learning scripting as a go. Do you think that most of my methods and ideas work? or could you tell?

    And will rewriting my script probably fix the problem?
     
  15. Swearsoft

    Swearsoft

    Joined:
    Mar 19, 2009
    Posts:
    1,632
    It's clear that the script is the problem, turn it off and try moving the block - you can't. Actually I have done this and more, even over a network and the character controller can't push things unless I specify it to do so. What is happening in your script though is not pushing, somehow the chasing and idle code intermingle, because you don't have a clear cut way of deciding what is happening and when.

    You have to at least divide the code into two section: one in which the enemy is aware of you and one in which it is not.
    Since your enemy has to see the player and before that he is doing his patrol, right?
     
  16. Swearsoft

    Swearsoft

    Joined:
    Mar 19, 2009
    Posts:
    1,632
    Oh, forgot to say the most important thing: you are using states, but you aren't using a switch case setup, which means you can be in more than one states during a frame, which is never going to work like you want it to.

    Go here: http://wiki.unity3d.com/index.php?title=UnityScript_Keywords

    Look under keyword 'switch' (keyword 'case', says look at 'switch')

    If you are in more than one states per frame your enemy doesn't know what to do, he might be following his path, chasing etc all at the same time, we don't want that do we?
     
  17. CG-DJ

    CG-DJ

    Joined:
    Jan 28, 2012
    Posts:
    73
    UPDATE (also amended to the first post):

    I'm back again... I totally rewrote the script. I took everything to the dry erase board and wrote it all out before I typed it up. The pushing problem still persists though. I can't figure out what's going wrong!!! Any ideas? (If you want, my skype is cg.artist4jc. If you contact request me, please put "Unity" in the contact request message.)

    |------------- LINK TO SCRIPT -------------|
     
  18. CG-DJ

    CG-DJ

    Joined:
    Jan 28, 2012
    Posts:
    73
    I tried increasing the drag and decreasing the mass of enemy's rigidbody. This helped a little, and it let me see the problem better. I think that the Character Controller (of the main character) is getting stuck inside the Collider (rigidbody) of the enemy when the enemy is moving.

    Lets say I'm moving right, but the enemy is moving left. I hit the enemy, and the enemy moves backwards, but (because of the upped drag) stays with my character. Now I am pushing the enemy right. However, if I turn and walk left, the enemy stays where it was. Then if I turn and walk right and run into the static enemy again, the enemy doesn't move anywhere, acting like a static object.

    I hope this explanation works...Any ideas on how to make sure the colliders don't get inside one another?
     
  19. CG-DJ

    CG-DJ

    Joined:
    Jan 28, 2012
    Posts:
    73
  20. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    Read my post in this thread about how you should be managing your various states. It uses the state design pattern and prevents state overlap.