- Home /
Smoother Rotation with mouse
I have a characterController where the Character rotates with Input.mousePosition.x Thats my Code using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements;
public class C2 : MonoBehaviour
{
public float Speed;
public float delta;
private float lastP;
float MousePos = Input.mousePosition.x;
private void Start()
{
Speed = 0.08f;
}
private void Update()
{
if (Input.GetButtonDown("Fire2"))
{
lastP = MousePos;
}
else if (Input.GetButton("Fire2"))
{
MousePos = Input.mousePosition.x;
delta = MousePos - lastP;
Debug.Log(delta);
transform.Rotate(0, delta * 0.5f, 0);
lastP = MousePos;
}
}
}
my Problem is that when i release fire2 and move my mouse and the click fire2 again it rotates to this position and its really unsoomth
pls help
Answer by waayne · Aug 05, 2020 at 07:41 PM
Hey, would be nice if you could tell us what you controller should do.
It should rotate the y axis of my player(when $$anonymous$$ouseButton2 is pressed and the cursor moved) Like in Roblox or other games
You could apply a camera as child to your player GameObject. Then you would need 2 scripts, one for camera and one for player. In the camera script you would rotate around the player using the mouse axis. In the player script you would snap the player rotation to the camera rotation if the right mouse button is pressed.
If you need further help, feel free to take a look at: http://waayne.com/index.php/unity-private-tutoring/
Answer by xxmariofer · Aug 06, 2020 at 01:03 PM
test this code
else if (Input.GetButton("Fire2"))
{
MousePos = Input.mousePosition.x;
delta = MousePos - lastP;
Debug.Log(delta);
Quaternion target = Quaternion.Euler(0, delta * 0.5f, 0);
transform.rotation = Quaternion.RotateTowards(transform.rotation, target, Speed);
lastP = MousePos;
}