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. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Resolved Telekinesis problem

Discussion in 'Physics' started by eljuanss, May 26, 2023.

  1. eljuanss

    eljuanss

    Joined:
    Sep 4, 2022
    Posts:
    4
    Hi everyone, I'm making a game for a university project. I have this gun, that impulse objects or platforms as telekinesis would do, like star wars force. The thing is, I can't manage to impulse objects in the desired direction. I'm trying to impulse where the camera is aiming, the normal of the raycast I think we could say, but it won't work. Tried many things, but the objects are impulsed to either wrong directions or just straight forward.

    This is my actual code:

    private void Update()
    {

    if (Input.GetMouseButtonDown(0)) {


    animator.SetTrigger("IsShooting");
    shot.Play();

    RaycastHit hit;
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    if (Physics.Raycast(ray, out hit))
    {
    if (hit.collider.CompareTag("Kinetic"))
    {
    Vector3 direccion = (hit.point - transform.position).normalized;
    Quaternion rotacion = Quaternion.LookRotation(direccion);
    Vector3 rotacionVector = rotacion.eulerAngles.normalized;

    Rigidbody rb = hit.collider.GetComponent<Rigidbody>();
    if (rb != null)
    {
    rb.AddForce(rotacionVector * fuerzaTelekinetica, ForceMode.Impulse);
    }
    }
    }

    }
    }
     
  2. eljuanss

    eljuanss

    Joined:
    Sep 4, 2022
    Posts:
    4
    Very dumb of me, I had activated an option that upon being disabled, telekinesis works just fine. The objects had the freeze Y position check, Uncheck and problem solved.