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 I am looking for a flexible way to rotate a gameobject around another

Discussion in 'Physics' started by paulberta14, Jun 19, 2022.

  1. paulberta14

    paulberta14

    Joined:
    Jun 1, 2021
    Posts:
    2
    I try to make an object rotate around another object on mouse input. In order to achieve this I used this code:

    Code (CSharp):
    1. transform.RotateAround(objectToRotateAround.transform.position, Vector3.back, Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed);
    I use GetAxisRaw because the movement has to be precise (the game that I am making requires a lot of reaction).

    The problem is the shield movement doesn't feel good. I would like to be able to add a little bit of friction to the movement.

    A solution that would enable me to tweak certain aspects of the rotation would be ideal. Any kind of guidance or documentation that might help is also very well-received .

    PS: I also tried to work with Quaternions. This was the code that I've tried.

    Code (CSharp):
    1. Vector3 relativePos = objectToRotateAround.transform.position - transform.position;
    2.        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(relativePos,Vector3.forward), Time.deltaTime);
    3.        transform.Translate(0, 0, Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed);
    There are a few problems with this code.

    1. The Quaternion.LookRotation rotates my object on all 3 axes instead of the Y-axis.
    2. I think even if the object rotates properly, the Translate method will make it move the same as the previous solution.
    Thank you for your time and attention. I really appreciate it.