Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Camera Rotator around a GameObject (Please Help)

Hello, my name's "hat".

Can someone please help me?

I was trying to make and find a script that would rotate the camera around a Object using the mouse, with no luck.

Can someone help me? Thanks!

-hat

Answers

  • This is a really, REALLY old one I made. Should be enough to get you started, I might update it and post it again later today.

    But until then, here you go: https://github.com/Mouledoux/360WebDemo/blob/master/Assets/Scripts/RotationToMouseTracking.cs

  • Mouledoux, thanks so much! But i meant like rotating the camera with you mouse.

  • MouledouxMouledoux Member
    edited June 29

    Yes.. that is what this script does

    Put this script on a camera, and it will rotate the camera. Give it an 'Anchor' and it will rotate around the anchor.

  • Yeah, but you need to click first? And it rotates the object, not the camera? Wait a sec

  • Just take the part where you need to click out. Yes it rotates the object.... the camera object. You can put this script on ANY object in unity, and it will rotate it. A camera is just another object.

  • hatoving_MGGhatoving_MGG Member
    edited June 29

    Ok, it works, but how can i make it so that it rotates the objects WHILE i'm moving the mouse? And not while holding a mouse button it down?

    I'm sorry if i'm asking so many questions, it's just that i'm still learning C# for Unity.

  • MouledouxMouledoux Member
    edited June 29

    Again, this is a old script, but if you want to take out the need to click and hold:

    1. Delete everything inside Update. Leave only the empty function.
    2. Change MoveToCursor to a void function, instead of an IEnumerator.
    3. Remove the while loops, and yield return nulls from MoveToCursor.
    4. Call this new MoveToCursor function, in the Update


    This script could definitely be optimized. As I said originally, I plan to redo and repost, hopefully later today.

  • I guess you're right, it needs to be re-done.

    Because now i can't move the camera. :/

  • MouledouxMouledoux Member
    edited June 29

    Yes, but this can still work. If you move initCursorPosition outside of the function and make it a member variable, and replace it inside the function with:

    "initCursorPos = Vector3.Lerp(initCursorPos, Input.mousePosition, Time.deltaTime);" it works


    The reason it didn't work, is because originally it got the change in mouse position based on the original click position. If the click position was always just where the mouse currently was, the difference was always 0, which means no movement. You can multiply Time.deltaTime here to make it more 'snappy' to the cursor.



    Edit: ALSO, remove "initCursorPos = Input.mousePosition;" from the bottom of each if statement.

Sign In or Register to comment.