Skip to main content
Tweeted twitter.com/StackGameDev/status/1185843237019443200
deleted 192 characters in body
Source Link
Venay
  • 207
  • 3
  • 12

I'm trying to do drag to rotate sort of thing, so far the code is fine. However when starting to drag (left clicking first), the object rotate to a random direction first which looks odd.

here is my code :

public Camera cam;
public float smoothAngle, dragPower = .3f;
public bool isRadial = true;
float angle, angleOld, angleDelta, angleRef;
Vector2 mousePosOld, mousePosDelta = Vector2.zero;

void Update()
{
  Vector2 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
  if (Input.GetMouseButton(0))
  {
    mousePosDelta = mousePos - mousePosOld; 
    float rotX = (mousePosDelta.magnitude / Time.deltaTime) 
                  * dragPower 
                  * -Mathf.Sign(mousePos.y) 
                  * Mathf.Sign(Vector2.Dot(Vector2.right, mousePosDelta));
      
    transform.Rotate(Vector3.forward, rotX);
    
    mousePosOld = mousePos;
    angleDelta = Mathf.DeltaAngle(angleOld, transform.localEulerAngles.z);
    angleOld = transform.localEulerAngles.z;
  }
  else
  {
    transform.RotateAround(transform.position, transform.forward, angleDelta);
    angleDelta = Mathf.SmoothDampAngle(angleDelta, 0, ref angleRef, smoothAngle);
  }
}
 
Vector2 rotated2D(Vector2 V, float a)
{
  return new Vector2(
      V.x * Mathf.Cos(a) - V.y * Mathf.Sin(a), 
      V.x * Mathf.Sin(a) + V.y * Mathf.Cos(a));
}

this is the effect :

enter image description here

I'm trying to do drag to rotate sort of thing, so far the code is fine. However when starting to drag (left clicking first), the object rotate to a random direction first which looks odd.

here is my code :

public Camera cam;
public float smoothAngle, dragPower = .3f;
public bool isRadial = true;
float angle, angleOld, angleDelta, angleRef;
Vector2 mousePosOld, mousePosDelta = Vector2.zero;

void Update()
{
  Vector2 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
  if (Input.GetMouseButton(0))
  {
    mousePosDelta = mousePos - mousePosOld; 
    float rotX = (mousePosDelta.magnitude / Time.deltaTime) 
                  * dragPower 
                  * -Mathf.Sign(mousePos.y) 
                  * Mathf.Sign(Vector2.Dot(Vector2.right, mousePosDelta));
      
    transform.Rotate(Vector3.forward, rotX);
    
    mousePosOld = mousePos;
    angleDelta = Mathf.DeltaAngle(angleOld, transform.localEulerAngles.z);
    angleOld = transform.localEulerAngles.z;
  }
  else
  {
    transform.RotateAround(transform.position, transform.forward, angleDelta);
    angleDelta = Mathf.SmoothDampAngle(angleDelta, 0, ref angleRef, smoothAngle);
  }
}
 
Vector2 rotated2D(Vector2 V, float a)
{
  return new Vector2(
      V.x * Mathf.Cos(a) - V.y * Mathf.Sin(a), 
      V.x * Mathf.Sin(a) + V.y * Mathf.Cos(a));
}

this is the effect :

enter image description here

I'm trying to do drag to rotate sort of thing, so far the code is fine. However when starting to drag (left clicking first), the object rotate to a random direction first which looks odd.

here is my code :

public Camera cam;
public float smoothAngle, dragPower = .3f;
public bool isRadial = true;
float angle, angleOld, angleDelta, angleRef;
Vector2 mousePosOld, mousePosDelta = Vector2.zero;

void Update()
{
  Vector2 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
  if (Input.GetMouseButton(0))
  {
    mousePosDelta = mousePos - mousePosOld; 
    float rotX = (mousePosDelta.magnitude / Time.deltaTime) 
                  * dragPower 
                  * -Mathf.Sign(mousePos.y) 
                  * Mathf.Sign(Vector2.Dot(Vector2.right, mousePosDelta));
      
    transform.Rotate(Vector3.forward, rotX);
    
    mousePosOld = mousePos;
    angleDelta = Mathf.DeltaAngle(angleOld, transform.localEulerAngles.z);
    angleOld = transform.localEulerAngles.z;
  }
  else
  {
    transform.RotateAround(transform.position, transform.forward, angleDelta);
    angleDelta = Mathf.SmoothDampAngle(angleDelta, 0, ref angleRef, smoothAngle);
  }
}

this is the effect :

enter image description here

Removed the need to scroll.
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 55
  • 61

Object Why does the object rotate to a random direction when iI start dragging [Unity]?

i'mI'm trying to do drag to rotate sort of thing, so far the code is fine. However when starting to drag (left clicking first), the object rotate to a random direction first which looks odd.

    public Camera cam;
    public float smoothAngle, dragPower = .3f;
    public bool isRadial = true;
    float angle, angleOld, angleDelta, angleRef;
    Vector2 mousePosOld, mousePosDelta = Vector2.zero;

    void Update()
    {



        Vector2 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
        if (Input.GetMouseButton(0))
        {
            
                
            
            mousePosDelta = mousePos - mousePosOld; 
            float rotX = (mousePosDelta.magnitude / Time.deltaTime) * dragPower * -Mathf.Sign(mousePos.y) * Mathf.Sign(Vector2.Dot(Vector2.right, mousePosDelta));
                
            transform.Rotate(Vector3.forward, rotX);
                
            
            mousePosOld = mousePos;
            angleDelta = Mathf.DeltaAngle(angleOld, transform.localEulerAngles.z);
            angleOld = transform.localEulerAngles.z;

            
        }
        else
        {
            transform.RotateAround(transform.position, transform.forward, angleDelta);
            angleDelta = Mathf.SmoothDampAngle(angleDelta, 0, ref angleRef, smoothAngle);
        }

    }

    Vector2 rotated2D(Vector2 V, float a)
    {
        return new Vector2(V.x * Mathf.Cos(a) - V.y * Mathf.Sin(a), V.x * Mathf.Sin(a) + V.y * Mathf.Cos(a));
    }
public Camera cam;
public float smoothAngle, dragPower = .3f;
public bool isRadial = true;
float angle, angleOld, angleDelta, angleRef;
Vector2 mousePosOld, mousePosDelta = Vector2.zero;

void Update()
{
  Vector2 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
  if (Input.GetMouseButton(0))
  {
    mousePosDelta = mousePos - mousePosOld; 
    float rotX = (mousePosDelta.magnitude / Time.deltaTime) 
                  * dragPower 
                  * -Mathf.Sign(mousePos.y) 
                  * Mathf.Sign(Vector2.Dot(Vector2.right, mousePosDelta));
      
    transform.Rotate(Vector3.forward, rotX);
    
    mousePosOld = mousePos;
    angleDelta = Mathf.DeltaAngle(angleOld, transform.localEulerAngles.z);
    angleOld = transform.localEulerAngles.z;
  }
  else
  {
    transform.RotateAround(transform.position, transform.forward, angleDelta);
    angleDelta = Mathf.SmoothDampAngle(angleDelta, 0, ref angleRef, smoothAngle);
  }
}

Vector2 rotated2D(Vector2 V, float a)
{
  return new Vector2(
      V.x * Mathf.Cos(a) - V.y * Mathf.Sin(a), 
      V.x * Mathf.Sin(a) + V.y * Mathf.Cos(a));
}

Object rotate to a random direction when i start dragging [Unity]

i'm trying to do drag to rotate sort of thing, so far the code is fine. However when starting to drag (left clicking first), the object rotate to a random direction first which looks odd.

    public Camera cam;
    public float smoothAngle, dragPower = .3f;
    public bool isRadial = true;
    float angle, angleOld, angleDelta, angleRef;
    Vector2 mousePosOld, mousePosDelta = Vector2.zero;

    void Update()
    {



        Vector2 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
        if (Input.GetMouseButton(0))
        {
            
                
            
            mousePosDelta = mousePos - mousePosOld; 
            float rotX = (mousePosDelta.magnitude / Time.deltaTime) * dragPower * -Mathf.Sign(mousePos.y) * Mathf.Sign(Vector2.Dot(Vector2.right, mousePosDelta));
                
            transform.Rotate(Vector3.forward, rotX);
                
            
            mousePosOld = mousePos;
            angleDelta = Mathf.DeltaAngle(angleOld, transform.localEulerAngles.z);
            angleOld = transform.localEulerAngles.z;

            
        }
        else
        {
            transform.RotateAround(transform.position, transform.forward, angleDelta);
            angleDelta = Mathf.SmoothDampAngle(angleDelta, 0, ref angleRef, smoothAngle);
        }

    }

    Vector2 rotated2D(Vector2 V, float a)
    {
        return new Vector2(V.x * Mathf.Cos(a) - V.y * Mathf.Sin(a), V.x * Mathf.Sin(a) + V.y * Mathf.Cos(a));
    }

Why does the object rotate to a random direction when I start dragging?

I'm trying to do drag to rotate sort of thing, so far the code is fine. However when starting to drag (left clicking first), the object rotate to a random direction first which looks odd.

public Camera cam;
public float smoothAngle, dragPower = .3f;
public bool isRadial = true;
float angle, angleOld, angleDelta, angleRef;
Vector2 mousePosOld, mousePosDelta = Vector2.zero;

void Update()
{
  Vector2 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
  if (Input.GetMouseButton(0))
  {
    mousePosDelta = mousePos - mousePosOld; 
    float rotX = (mousePosDelta.magnitude / Time.deltaTime) 
                  * dragPower 
                  * -Mathf.Sign(mousePos.y) 
                  * Mathf.Sign(Vector2.Dot(Vector2.right, mousePosDelta));
      
    transform.Rotate(Vector3.forward, rotX);
    
    mousePosOld = mousePos;
    angleDelta = Mathf.DeltaAngle(angleOld, transform.localEulerAngles.z);
    angleOld = transform.localEulerAngles.z;
  }
  else
  {
    transform.RotateAround(transform.position, transform.forward, angleDelta);
    angleDelta = Mathf.SmoothDampAngle(angleDelta, 0, ref angleRef, smoothAngle);
  }
}

Vector2 rotated2D(Vector2 V, float a)
{
  return new Vector2(
      V.x * Mathf.Cos(a) - V.y * Mathf.Sin(a), 
      V.x * Mathf.Sin(a) + V.y * Mathf.Cos(a));
}
Source Link
Venay
  • 207
  • 3
  • 12

Object rotate to a random direction when i start dragging [Unity]

i'm trying to do drag to rotate sort of thing, so far the code is fine. However when starting to drag (left clicking first), the object rotate to a random direction first which looks odd.

here is my code :

    public Camera cam;
    public float smoothAngle, dragPower = .3f;
    public bool isRadial = true;
    float angle, angleOld, angleDelta, angleRef;
    Vector2 mousePosOld, mousePosDelta = Vector2.zero;

    void Update()
    {



        Vector2 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
        if (Input.GetMouseButton(0))
        {
            
                
            
            mousePosDelta = mousePos - mousePosOld; 
            float rotX = (mousePosDelta.magnitude / Time.deltaTime) * dragPower * -Mathf.Sign(mousePos.y) * Mathf.Sign(Vector2.Dot(Vector2.right, mousePosDelta));
                
            transform.Rotate(Vector3.forward, rotX);
                
            
            mousePosOld = mousePos;
            angleDelta = Mathf.DeltaAngle(angleOld, transform.localEulerAngles.z);
            angleOld = transform.localEulerAngles.z;

            
        }
        else
        {
            transform.RotateAround(transform.position, transform.forward, angleDelta);
            angleDelta = Mathf.SmoothDampAngle(angleDelta, 0, ref angleRef, smoothAngle);
        }

    }

    Vector2 rotated2D(Vector2 V, float a)
    {
        return new Vector2(V.x * Mathf.Cos(a) - V.y * Mathf.Sin(a), V.x * Mathf.Sin(a) + V.y * Mathf.Cos(a));
    }

this is the effect :

enter image description here