Search Unity

Trying to get player pulled to certain point smoothly

Discussion in 'Scripting' started by Durins-Bane, Feb 26, 2017.

  1. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. //using UnityEngine.Networking;
    5.  
    6. public class SpellExplosionEffects : MonoBehaviour {
    7.     float forceAmount = 50;
    8.     Rigidbody r;
    9.     float radi;
    10.     float spellDuration = 10;
    11.     // Use this for initialization
    12.     void Awake()
    13.     {
    14.             radi = GetComponent<SphereCollider>().radius;
    15.             Destroy(gameObject, spellDuration);
    16.             transform.position = new Vector3(transform.position.x, transform.position.y + 1, transform.position.z);
    17.    
    18.     }
    19.         private void OnTriggerEnter(Collider other)    {
    20.    
    21.             if (other == null)
    22.                 return;
    23.             else
    24.             if(other.GetComponent<PlayerMove>() != null)
    25.                 r = other.GetComponent<Rigidbody>();
    26.  
    27.         }
    28.  
    29.  
    30.         private void FixedUpdate()    {
    31.      
    32.             if (r != null){
    33.                 var direction = Vector3.zero;
    34.                 if (Vector3.Distance(transform.position, r.position) < radi)                {
    35.                     direction = (transform.position - r.position).normalized;
    36.                     r.AddRelativeForce(direction * 30, ForceMode.Force);
    37.                    // r = null;
    38.                    // Debug.LogError("PLAYER ENTERED SPELL RADIUS");
    39.             }
    40.         }
    41.     }
    42. }
    43.  

    Does anyone know what might be wrong?

    It works, it effects the players but sometimes it will repel them instead of pulling them towards the centre of the object.

    Sometimes it will only pull players in from one side

    This should work if you want to test it just add it to an empty game object with a sphere collider and then add a player which is controlled using the rigid body and when it gets within range it will effect the player but not how I would've thought it would.




    You see how the blue spell only works sometimes it should pull the player towards the centre slowing down their movement
     
    Last edited: Feb 26, 2017
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Do you have more than one player that this could be pulling in? OnTriggerEnter will fire for every player that enters its trigger radius, overwriting R each time. If you have more than 1 "player" that could be affected by this object you would need to change r to a List<RigidBody> and keep adding to that last. Then in FixedUpdate iterate over that list, add all the forces, then clear the list. Also you need to set r= null regardless or it will keep adding forces over and over.

    Now the reason your getting wonky behaviour is your using AddRelativeForce which treats the force relative to its local rotation. But you using absolute world co-ordinates. So if your spell is turned in any way different from the world forward/up/right then it will act weird. Try changing to just AddForce
     
    Durins-Bane likes this.
  3. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    Thank you I will try this now!

    And the first part, no there is only one player within each its works exactly the same with two players :)
     
  4. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    Wow, changing to AddForce makes it work perfectly TYVM!
    :D
     
    takatok likes this.