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

Adding Force towards a specific point

Discussion in 'Editor & General Support' started by axmanvt16, Jun 24, 2019.

  1. axmanvt16

    axmanvt16

    Joined:
    Sep 13, 2018
    Posts:
    7
    Hi there,
    I had a question regarding pointing the rigid-body AddForce in the direction of an object. Rather that using a calculation for forward or any local vectors, I'm trying to wrap my head around what I need to do to get my player character to be blasted off towards another objects position.

    The idea is that the player is using a grappling hook in zero gravity so the hook would reach its destination after a delay and then it would pull the player to the position that the hook landed. Ive managed to link scripts and fire the hook to a position and send back its position to the player character once it has made contact with something. The only problem im running into now is how do I take that position and have the player be pulled towards that location regardless of where they are or where they are facing at that time.

    any ideas on how I should start? I very much appreciate any help. Thanks!
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
  3. axmanvt16

    axmanvt16

    Joined:
    Sep 13, 2018
    Posts:
    7
    Well the projectile gets there, I'm trying to get the direction that the projectile is in and then apply force in that direction to the player character. Thanks for the response though.
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Sorry, I misread your original post, or read it too quickly.

    It seems like what you're trying to do is generally as simple as just applying force in the exact line from the player to the anchor point. I mean, that's all a device like this could do anyway in real like. As you real it in, it simply shortens the distance between the player and the anchor, pulling the player towards the anchor. In that case, the force direction is simple `(AnchorPosition - PlayerPosition).normalized` That's the straightforward approach, but it seems simple enough that maybe you've tried that, and run into problems?

    This won't address the potential need to rotate the player to face the anchor, but that could be managed with Vector3.RotateTowards to make the player aim in the same direction as the force vector.

    Things will get a little wild as you get close to the anchor, especially if you keep any momentum you had before you started the grapple. Maybe the player just stops when they get close enough.
     
  5. axmanvt16

    axmanvt16

    Joined:
    Sep 13, 2018
    Posts:
    7
    that does work! however im now trying to simply reverse the process to return the hook to the player but somehow the force is being applied to both the hook and the player at the same time.


    void Update()
    {
    if (Hook != null)
    {
    // direction player is launched twards
    Vector3 hookPos = (Hook.transform.position - transform.position).normalized;
    // direction hook is launched twards
    Vector3 hookreturn = (transform.position - Hook.transform.position).normalized;

    //modifying the hook angle for return
    Rigidbody rig_Hook = Hook.GetComponent<Rigidbody>();
    Hook.transform.forward = hookreturn;

    //modifying the player for boost
    rig.velocity = hookPos;
    rig.AddForce(rig.velocity * 30, ForceMode.Impulse);


    if (Input.GetMouseButtonDown(1))
    {

    rig_Hook.isKinematic = false;
    rig_Hook.useGravity = false;
    // sending the hook back to the player
    rig_Hook.velocity = hookreturn * 10;
    Hook.GetComponent<SphereCollider>().isTrigger = true;
    Hook.GetComponent<SphereCollider>().enabled = true;


    }
    }
    }


    i cannot for the life of make sense as to why. alot of other weird stuff happens when i try to return the hook but its unrelated to this problem.
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Do you have the hook connected to the player via a joint? If so, then force applied to the hook might be pushing the player.

    Is there a big collider associated with the hook, which is pushing the player back?

    By the way, you shouldn't be calling AddForce within Update(). Physics forces are generally applied in FixedUpdate(). But watch out, because checking for input (like mouse down) should still be done in Update, not FixedUpdate();
     
  7. axmanvt16

    axmanvt16

    Joined:
    Sep 13, 2018
    Posts:
    7
    oh yea, i moved it out of a function to test if it was an update issue. it wasnt. the bullet isnt connected to the player with the exception of a rig reference from the hook itself. heres the rest of the code:

    public class ProjectileSpawner : MonoBehaviour
    {

    public GameObject projectile;
    public GameObject projectileSpawnerPos;
    public GameObject grappleObject;
    public bool hasShot;
    private GameObject bullet;




    public void SpawnProjectile()
    {
    if (!hasShot)
    {

    bullet = Instantiate(projectile, projectileSpawnerPos.transform.position, projectileSpawnerPos.transform.rotation * Quaternion.Euler(0, -90, 0)) as GameObject;
    bullet.GetComponent<Rigidbody>().AddForce(projectileSpawnerPos.transform.forward * 4000);
    hasShot = true;


    }
    }

    private void Update()
    {
    if (Input.GetButtonDown("Fire1"))
    {
    grappleObject.SetActive(false);
    SpawnProjectile();


    }


    }
    }


    this is the code for the spawn point for the bullet, and below is the code for the bullet.


    public GameObject Player;
    public GameObject spawner;
    public bool hasHooked;

    IEnumerator killTimer()
    {
    yield return new WaitForSeconds(0.5f);

    if(hasHooked == false)
    {
    print("Times up");
    Destroy(this.gameObject);
    spawner.GetComponent<ProjectileSpawner>().hasShot = false;
    spawner.GetComponent<ProjectileSpawner>().grappleObject.SetActive(true);
    }

    }

    private void Start()
    {

    hasHooked = false;

    }

    private void Awake()
    {

    Player = GameObject.Find("Player");
    spawner = GameObject.Find("bulletSpawn");
    StartCoroutine(killTimer());

    }



    private void OnCollisionEnter(Collision collision)
    {

    if (collision.gameObject.tag != "Player")
    {
    hasHooked = true;
    gameObject.GetComponent<Rigidbody>().isKinematic = true;
    gameObject.GetComponent<Collider>().enabled = false;
    Player.GetComponentInChildren<pl_GrapplingHook>().Hook = this.gameObject;


    }


    }

    private void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.tag == "Player")
    {
    spawner.GetComponentInChildren<ProjectileSpawner>().hasShot = false;
    Player.GetComponentInChildren<pl_GrapplingHook>().grappleObject.SetActive(true);
    Player.GetComponentInChildren<pl_GrapplingHook>().rangeOut = false;
    Destroy(this.gameObject);



    }
    }

     
  8. axmanvt16

    axmanvt16

    Joined:
    Sep 13, 2018
    Posts:
    7
    oh, the first script is however connected to the player character