Search Unity

Invisible walls around player which object can pass through

Discussion in 'Scripting' started by unity3dat, Oct 14, 2017.

  1. unity3dat

    unity3dat

    Joined:
    Aug 9, 2017
    Posts:
    88
    (typo in the title: should be: "Invisible walls around player which object CAN'T pass through")

    I'm looking for a way to accomplish the following:

    I have an object which moves randomly around in the level. I made this script for this behaviour:



    (top view of the 3D environment)

    Code (CSharp):
    1.  
    2. void Start()
    3. {
    4. InvokeRepeating ("MoveTarget", 1, UnityEngine.Random.Range (0.1f, 1f));
    5. }
    6.  
    7. void Update()
    8. {
    9. transform.position = Vector3.MoveTowards(transform.position, newPosition, Time.deltaTime*7);
    10. }
    11.  
    12. void MoveTarget()
    13. {
    14.             var x = UnityEngine.Random.Range (-100, 100);
    15.             var y = UnityEngine.Random.Range (1, 6);
    16.             var z = UnityEngine.Random.Range (-100, 100);
    17.             newPosition = new Vector3 (x, y, z);
    18. }
    In this level, the user controls a first person character (not a rigidbody, but via the CharacterController).

    This works perfectly, but I don't want the object to come to close to the character. So I thought about adding invisible walls around the player which follow the player, and where the object can't pass through. I can't get this to work smoothly. No matter what I try, it's either glitchy, or the object can pass through the walls the moment the walls are in motion. I don't wish to expand the pill-shaped collider of my character because it shouldn't keep as much distance from, for example, walls. Only from the green object. The character should always be in the center of the walls too, of course.

    Anyone who can point me in the right direction?
     
    Last edited: Oct 20, 2017
  2. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    I'm not sure on what your movement code looks like, but you can calculate the distance between the object and the player, and never let it drop below a certain amount? This would obviously result in a circular wall, but would that be a problem for what you need?
     
  3. unity3dat

    unity3dat

    Joined:
    Aug 9, 2017
    Posts:
    88
    Thanks for the suggestion. I don't mind a circular wall, but I don't know exactly how I could implement your suggestion. I could calculate the distance, but what to do at the critical point? Steer the object away? How to determine what "away" is? And what if the users keeps following it (and walks faster then the object moves)?
     
  4. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    Those things depends entirely on your own game design I'm afraid. What would you want to happen?

    I use this circular wall for stopping my enemies different distances from my player depending on their attack range... and then once at that range they will initiate an attack. As soon as the player moves, they will continue to follow the player until they reach the wall again.

    If you give some insight into what you want to achieve overall I'll gladly offer some suggestions
     
  5. unity3dat

    unity3dat

    Joined:
    Aug 9, 2017
    Posts:
    88
    I see. Basically, the green object is a flying target which the character can only kill with a laser beam. The character has to keep his crosshair as long as possible on the flying target to kill it. During this process, the character is able to move (for example follow the object), and the object flies randomly in the level.

    Let's say the object flies towards the character, and the character walks towards the object. In that case, the object shouldn't be able to "come too close". It's fine for me that it just hits the invisible wall. If the character keeps moving forwards, it should push the object backwards of course (but without "kicking it" away. It should merely "block" it). Another approach would be to steer it away from the character. Both are fine.
     
  6. unity3dat

    unity3dat

    Joined:
    Aug 9, 2017
    Posts:
    88
    Any ideas? :)
     
  7. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    Sorry for the long reply, been out of action for a while!

    Do you already have a way of telling the distance between your player and the enemy object?

    Code (CSharp):
    1. private int distance; //between object and player
    2. private int minProximity = 5;
    3.  
    4. private void Update ()
    5. {
    6. //However you calculate your distance (raycast perhaps?)
    7. while (distance < minProximity)
    8. {
    9. //Do some other movement
    10. } else
    11. {
    12. //Normal movement code
    13. }
    14. }
    I would use a while loop like above to perhaps change the movement behaviour of your flying object while it is within a certain distance of your player. You can use AddForce to the flying object when it reaches this distance (needs to be a rigidbody). Also, can you use a collider on your player object?
     
  8. unity3dat

    unity3dat

    Joined:
    Aug 9, 2017
    Posts:
    88
    Adding a collider to the player didn't work, unfortunately.

    I'm able to calculate the distance between the player and the enemy object. The problem is that the player moves faster than the object. I could steer away the object, but the player will still be able to come close if it would follow the object.

    I have a RigidBody on the object (but without gravity). How would adding force work? Wouldn't this "flick" the object away, instead of moving it away? The force should only be applied if the player is too close. I'm pretty new to Unity so I can't really figure this one out...
     
  9. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Have onTriggerEnter code on the ball that will reverse travel direction of the ball if it enters the trigger on the wall around the player. Or just rotates it 180 degrees so it will travel the opposite direction.
     
  10. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    He would still have the problem of the object moving slower than the player if he does that. Apologies but I don't think I'm able to help. Still fairly newish to unity myself
     
  11. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Put a sphere collider on your flying enemy set it to be a trigger. (When the player enters this we move away.)

    Add an OnTriggerEnter call to your script.

    When the player enters (You can use tags to check if its the player) recalculate the position (Do the flying objects position - the players position to get the direction away from it) taking into account the direction away from the player.

    Have two speeds, patrol & run. Use patrol when the position is recalculated normally & run when the player causes a recalculation.