Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Telekinesis problem

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

  1. eljuanss

    eljuanss

    Joined:
    Sep 4, 2022
    Posts:
    5
    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:
    5
    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.