How to rotate an object along all 3 axes with finger swipe?











up vote
4
down vote

favorite
1












I'm working on a simple AR Vuforia application. How can I implement the rotation of an object along all three axes with one finger swipe?



The code I'm currently using has one bug: the rotation of the object depends on its local axes. For example, if I look at the object from the front, everything works as it should, but if I look at the object from the back side, the finger swipe upwards makes it rotate downwards and vice versa.



Here is this script:



public float rotSpeed = 30f;

void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y")*rotSpeed*Mathf.Deg2Rad;

transform.Rotate(Vector3.up, -rotX);
transform.Rotate(Vector3.right, -rotY);
}


This is not what I need, how can I rotate the object according to the finger swipe direction regardless of the angle from which I look at it?



UPD:

A simple non-AR example, which may help you understand what I need is an ios game "Space Frontier 2". After a successful launch of the rocket, it lands on the planet and you can rotate the planet with your finger swipe.



HERE IS THE VIDEO DEMO:
https://youtu.be/OiNPP1WNIAI










share|improve this question
























  • So here's what comes to mind for me - how do you want to accomplish having forces with a single force acting on the object? To accomplish movement on more than one axis - we need more than one force, eg, either 1 simulated force ( maybe use the rotation/tilt/orientation/compass direction of the device? ) AND the single finger swipe. Think of physical objects - if I grab my phone that's sitting on my desk right now, and move it along the X axis (eg, paralell to my desk, or even diagonal) - it's not ever using that 3rd axis till I start twisting my wrist; adding that extra movement to it.
    – Kasem O
    Nov 2 at 14:42












  • @KasemO Thank you for an interesting idea, though I can't say that understood it completely yet. I don't need to move my phone round to rotate the object on the screen, because this is an AR app, and the user will be looking at the 3D model over the marker. I added a small UPD with he video demo which might help you understand what I mean. Thank you ones again!
    – Rumata
    Nov 2 at 15:05










  • Did you try out my answer yet? It's compact and works in all cases.
    – Thomas Hilbert
    Nov 2 at 16:17










  • @ThomasHilbert Thank you very much, it works perfectly!
    – Rumata
    Nov 2 at 16:52















up vote
4
down vote

favorite
1












I'm working on a simple AR Vuforia application. How can I implement the rotation of an object along all three axes with one finger swipe?



The code I'm currently using has one bug: the rotation of the object depends on its local axes. For example, if I look at the object from the front, everything works as it should, but if I look at the object from the back side, the finger swipe upwards makes it rotate downwards and vice versa.



Here is this script:



public float rotSpeed = 30f;

void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y")*rotSpeed*Mathf.Deg2Rad;

transform.Rotate(Vector3.up, -rotX);
transform.Rotate(Vector3.right, -rotY);
}


This is not what I need, how can I rotate the object according to the finger swipe direction regardless of the angle from which I look at it?



UPD:

A simple non-AR example, which may help you understand what I need is an ios game "Space Frontier 2". After a successful launch of the rocket, it lands on the planet and you can rotate the planet with your finger swipe.



HERE IS THE VIDEO DEMO:
https://youtu.be/OiNPP1WNIAI










share|improve this question
























  • So here's what comes to mind for me - how do you want to accomplish having forces with a single force acting on the object? To accomplish movement on more than one axis - we need more than one force, eg, either 1 simulated force ( maybe use the rotation/tilt/orientation/compass direction of the device? ) AND the single finger swipe. Think of physical objects - if I grab my phone that's sitting on my desk right now, and move it along the X axis (eg, paralell to my desk, or even diagonal) - it's not ever using that 3rd axis till I start twisting my wrist; adding that extra movement to it.
    – Kasem O
    Nov 2 at 14:42












  • @KasemO Thank you for an interesting idea, though I can't say that understood it completely yet. I don't need to move my phone round to rotate the object on the screen, because this is an AR app, and the user will be looking at the 3D model over the marker. I added a small UPD with he video demo which might help you understand what I mean. Thank you ones again!
    – Rumata
    Nov 2 at 15:05










  • Did you try out my answer yet? It's compact and works in all cases.
    – Thomas Hilbert
    Nov 2 at 16:17










  • @ThomasHilbert Thank you very much, it works perfectly!
    – Rumata
    Nov 2 at 16:52













up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





I'm working on a simple AR Vuforia application. How can I implement the rotation of an object along all three axes with one finger swipe?



The code I'm currently using has one bug: the rotation of the object depends on its local axes. For example, if I look at the object from the front, everything works as it should, but if I look at the object from the back side, the finger swipe upwards makes it rotate downwards and vice versa.



Here is this script:



public float rotSpeed = 30f;

void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y")*rotSpeed*Mathf.Deg2Rad;

transform.Rotate(Vector3.up, -rotX);
transform.Rotate(Vector3.right, -rotY);
}


This is not what I need, how can I rotate the object according to the finger swipe direction regardless of the angle from which I look at it?



UPD:

A simple non-AR example, which may help you understand what I need is an ios game "Space Frontier 2". After a successful launch of the rocket, it lands on the planet and you can rotate the planet with your finger swipe.



HERE IS THE VIDEO DEMO:
https://youtu.be/OiNPP1WNIAI










share|improve this question















I'm working on a simple AR Vuforia application. How can I implement the rotation of an object along all three axes with one finger swipe?



The code I'm currently using has one bug: the rotation of the object depends on its local axes. For example, if I look at the object from the front, everything works as it should, but if I look at the object from the back side, the finger swipe upwards makes it rotate downwards and vice versa.



Here is this script:



public float rotSpeed = 30f;

void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y")*rotSpeed*Mathf.Deg2Rad;

transform.Rotate(Vector3.up, -rotX);
transform.Rotate(Vector3.right, -rotY);
}


This is not what I need, how can I rotate the object according to the finger swipe direction regardless of the angle from which I look at it?



UPD:

A simple non-AR example, which may help you understand what I need is an ios game "Space Frontier 2". After a successful launch of the rocket, it lands on the planet and you can rotate the planet with your finger swipe.



HERE IS THE VIDEO DEMO:
https://youtu.be/OiNPP1WNIAI







c# unity3d augmented-reality vuforia






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 1:52









a stone arachnid

5451520




5451520










asked Oct 27 at 23:08









Rumata

175218




175218












  • So here's what comes to mind for me - how do you want to accomplish having forces with a single force acting on the object? To accomplish movement on more than one axis - we need more than one force, eg, either 1 simulated force ( maybe use the rotation/tilt/orientation/compass direction of the device? ) AND the single finger swipe. Think of physical objects - if I grab my phone that's sitting on my desk right now, and move it along the X axis (eg, paralell to my desk, or even diagonal) - it's not ever using that 3rd axis till I start twisting my wrist; adding that extra movement to it.
    – Kasem O
    Nov 2 at 14:42












  • @KasemO Thank you for an interesting idea, though I can't say that understood it completely yet. I don't need to move my phone round to rotate the object on the screen, because this is an AR app, and the user will be looking at the 3D model over the marker. I added a small UPD with he video demo which might help you understand what I mean. Thank you ones again!
    – Rumata
    Nov 2 at 15:05










  • Did you try out my answer yet? It's compact and works in all cases.
    – Thomas Hilbert
    Nov 2 at 16:17










  • @ThomasHilbert Thank you very much, it works perfectly!
    – Rumata
    Nov 2 at 16:52


















  • So here's what comes to mind for me - how do you want to accomplish having forces with a single force acting on the object? To accomplish movement on more than one axis - we need more than one force, eg, either 1 simulated force ( maybe use the rotation/tilt/orientation/compass direction of the device? ) AND the single finger swipe. Think of physical objects - if I grab my phone that's sitting on my desk right now, and move it along the X axis (eg, paralell to my desk, or even diagonal) - it's not ever using that 3rd axis till I start twisting my wrist; adding that extra movement to it.
    – Kasem O
    Nov 2 at 14:42












  • @KasemO Thank you for an interesting idea, though I can't say that understood it completely yet. I don't need to move my phone round to rotate the object on the screen, because this is an AR app, and the user will be looking at the 3D model over the marker. I added a small UPD with he video demo which might help you understand what I mean. Thank you ones again!
    – Rumata
    Nov 2 at 15:05










  • Did you try out my answer yet? It's compact and works in all cases.
    – Thomas Hilbert
    Nov 2 at 16:17










  • @ThomasHilbert Thank you very much, it works perfectly!
    – Rumata
    Nov 2 at 16:52
















So here's what comes to mind for me - how do you want to accomplish having forces with a single force acting on the object? To accomplish movement on more than one axis - we need more than one force, eg, either 1 simulated force ( maybe use the rotation/tilt/orientation/compass direction of the device? ) AND the single finger swipe. Think of physical objects - if I grab my phone that's sitting on my desk right now, and move it along the X axis (eg, paralell to my desk, or even diagonal) - it's not ever using that 3rd axis till I start twisting my wrist; adding that extra movement to it.
– Kasem O
Nov 2 at 14:42






So here's what comes to mind for me - how do you want to accomplish having forces with a single force acting on the object? To accomplish movement on more than one axis - we need more than one force, eg, either 1 simulated force ( maybe use the rotation/tilt/orientation/compass direction of the device? ) AND the single finger swipe. Think of physical objects - if I grab my phone that's sitting on my desk right now, and move it along the X axis (eg, paralell to my desk, or even diagonal) - it's not ever using that 3rd axis till I start twisting my wrist; adding that extra movement to it.
– Kasem O
Nov 2 at 14:42














@KasemO Thank you for an interesting idea, though I can't say that understood it completely yet. I don't need to move my phone round to rotate the object on the screen, because this is an AR app, and the user will be looking at the 3D model over the marker. I added a small UPD with he video demo which might help you understand what I mean. Thank you ones again!
– Rumata
Nov 2 at 15:05




@KasemO Thank you for an interesting idea, though I can't say that understood it completely yet. I don't need to move my phone round to rotate the object on the screen, because this is an AR app, and the user will be looking at the 3D model over the marker. I added a small UPD with he video demo which might help you understand what I mean. Thank you ones again!
– Rumata
Nov 2 at 15:05












Did you try out my answer yet? It's compact and works in all cases.
– Thomas Hilbert
Nov 2 at 16:17




Did you try out my answer yet? It's compact and works in all cases.
– Thomas Hilbert
Nov 2 at 16:17












@ThomasHilbert Thank you very much, it works perfectly!
– Rumata
Nov 2 at 16:52




@ThomasHilbert Thank you very much, it works perfectly!
– Rumata
Nov 2 at 16:52












3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted
+200










This works nice regardless of your object's rotation, and regardless of your camera position relative to the object:



public float rotSpeed = 30f;

void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed;

Camera camera = Camera.main;

Vector3 right = Vector3.Cross(camera.transform.up, transform.position - camera.transform.position);
Vector3 up = Vector3.Cross(transform.position - camera.transform.position, right);

transform.rotation = Quaternion.AngleAxis(-rotX, up) * transform.rotation;
transform.rotation = Quaternion.AngleAxis(rotY, right) * transform.rotation;
}


Make sure your camera has the "MainCamera" tag, or assign the camera externally if necessary.






share|improve this answer























  • Thank you very much, this code works really great! I will be able to award the bounty in 21 hours.
    – Rumata
    Nov 2 at 16:58




















up vote
1
down vote













I do not have the exact code with me right now.



But if you did not update your point of view coordinates, this should be the expected result.



Consider a real ball with 2 colors, blue and red, separated vertically.
When you are in front of it, seeing only the blue side, stroking it up will make the blue side go up and the red side appear from the bottom.
Now move behind it, seeing only the red side, and stroke it up again.
The blue face will go down and appear from the bottom.
Unity applies physics to virtual objects the same way we interact with real objects.



So you need to consider your camera position with the object orientation when you apply movements to it.
You need to apply a transformation matrix to your movement based on your camera location related to the object origin orientation.



I hope this is clear enough to put you on tracks to fix it.






share|improve this answer





















  • This sounds like what I need, thank you! I would be grateful if you could add the code to your answer so that I could test it. Also, I added a small UPD in the question description, which might also be helpful as an example.
    – Rumata
    Nov 2 at 14:56












  • And I also added a video demo as an illustration.
    – Rumata
    Nov 2 at 15:06










  • Take a look at the very good answer/explanations by DMGregory in this link gamedev.stackexchange.com/questions/136174/…
    – Pic Mickael
    Nov 2 at 15:07


















up vote
1
down vote













I think you have to somehow clamp the rotation to have the desired behaviour. I wrote a script recently to do just that. I did a little modification though.



public float rotSpeed = 30f;

float ClampAngle(float _angle, float _min, float _max)
{
if (_angle < 0f) _angle = 360 + _angle;
if (_angle > 180f) Mathf.Max(_angle, 360 + _min);
return Mathf.Min(_angle, _max);
}


USAGE:



void RotateGameObject()
{
float h = Input.GetTouch(0).deltaPosition.x * Time.deltaTime * rotSpeed*Mathf.Deg2Rad;
float v = Input.GetTouch(0).deltaPosition.y * Time.deltaTime * rotSpeed*Mathf.Deg2Rad;

Vector3 rot = transform.rotation.eulerAngles + new Vector3(-v, h, 0f);
//Change the y & z values to match your expected behaviour.
rot.x = ClampAngle(rot.x, -5f, 20f);
//Clamp rotation on the y-axis
rot.y = ClampAngle(rot.y, -20f, 20f);
transform.eulerAngles = rot;
}


See if that works and of course, try to play with the values.






share|improve this answer





















  • Thank you for an interesting example, I tried to call RotateGameObject() function OnMouseDrag(), it works in a bit strange way - the rotation of the object "jumps" to the default state several times during the finger swipe. Please let me know if I'm using it wrong. Still very interesting code, I can learn a lot from it, thanks!
    – Rumata
    Nov 2 at 16:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53027055%2fhow-to-rotate-an-object-along-all-3-axes-with-finger-swipe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted
+200










This works nice regardless of your object's rotation, and regardless of your camera position relative to the object:



public float rotSpeed = 30f;

void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed;

Camera camera = Camera.main;

Vector3 right = Vector3.Cross(camera.transform.up, transform.position - camera.transform.position);
Vector3 up = Vector3.Cross(transform.position - camera.transform.position, right);

transform.rotation = Quaternion.AngleAxis(-rotX, up) * transform.rotation;
transform.rotation = Quaternion.AngleAxis(rotY, right) * transform.rotation;
}


Make sure your camera has the "MainCamera" tag, or assign the camera externally if necessary.






share|improve this answer























  • Thank you very much, this code works really great! I will be able to award the bounty in 21 hours.
    – Rumata
    Nov 2 at 16:58

















up vote
3
down vote



accepted
+200










This works nice regardless of your object's rotation, and regardless of your camera position relative to the object:



public float rotSpeed = 30f;

void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed;

Camera camera = Camera.main;

Vector3 right = Vector3.Cross(camera.transform.up, transform.position - camera.transform.position);
Vector3 up = Vector3.Cross(transform.position - camera.transform.position, right);

transform.rotation = Quaternion.AngleAxis(-rotX, up) * transform.rotation;
transform.rotation = Quaternion.AngleAxis(rotY, right) * transform.rotation;
}


Make sure your camera has the "MainCamera" tag, or assign the camera externally if necessary.






share|improve this answer























  • Thank you very much, this code works really great! I will be able to award the bounty in 21 hours.
    – Rumata
    Nov 2 at 16:58















up vote
3
down vote



accepted
+200







up vote
3
down vote



accepted
+200




+200




This works nice regardless of your object's rotation, and regardless of your camera position relative to the object:



public float rotSpeed = 30f;

void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed;

Camera camera = Camera.main;

Vector3 right = Vector3.Cross(camera.transform.up, transform.position - camera.transform.position);
Vector3 up = Vector3.Cross(transform.position - camera.transform.position, right);

transform.rotation = Quaternion.AngleAxis(-rotX, up) * transform.rotation;
transform.rotation = Quaternion.AngleAxis(rotY, right) * transform.rotation;
}


Make sure your camera has the "MainCamera" tag, or assign the camera externally if necessary.






share|improve this answer














This works nice regardless of your object's rotation, and regardless of your camera position relative to the object:



public float rotSpeed = 30f;

void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed;

Camera camera = Camera.main;

Vector3 right = Vector3.Cross(camera.transform.up, transform.position - camera.transform.position);
Vector3 up = Vector3.Cross(transform.position - camera.transform.position, right);

transform.rotation = Quaternion.AngleAxis(-rotX, up) * transform.rotation;
transform.rotation = Quaternion.AngleAxis(rotY, right) * transform.rotation;
}


Make sure your camera has the "MainCamera" tag, or assign the camera externally if necessary.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 2 at 15:49

























answered Nov 2 at 15:21









Thomas Hilbert

2,6502627




2,6502627












  • Thank you very much, this code works really great! I will be able to award the bounty in 21 hours.
    – Rumata
    Nov 2 at 16:58




















  • Thank you very much, this code works really great! I will be able to award the bounty in 21 hours.
    – Rumata
    Nov 2 at 16:58


















Thank you very much, this code works really great! I will be able to award the bounty in 21 hours.
– Rumata
Nov 2 at 16:58






Thank you very much, this code works really great! I will be able to award the bounty in 21 hours.
– Rumata
Nov 2 at 16:58














up vote
1
down vote













I do not have the exact code with me right now.



But if you did not update your point of view coordinates, this should be the expected result.



Consider a real ball with 2 colors, blue and red, separated vertically.
When you are in front of it, seeing only the blue side, stroking it up will make the blue side go up and the red side appear from the bottom.
Now move behind it, seeing only the red side, and stroke it up again.
The blue face will go down and appear from the bottom.
Unity applies physics to virtual objects the same way we interact with real objects.



So you need to consider your camera position with the object orientation when you apply movements to it.
You need to apply a transformation matrix to your movement based on your camera location related to the object origin orientation.



I hope this is clear enough to put you on tracks to fix it.






share|improve this answer





















  • This sounds like what I need, thank you! I would be grateful if you could add the code to your answer so that I could test it. Also, I added a small UPD in the question description, which might also be helpful as an example.
    – Rumata
    Nov 2 at 14:56












  • And I also added a video demo as an illustration.
    – Rumata
    Nov 2 at 15:06










  • Take a look at the very good answer/explanations by DMGregory in this link gamedev.stackexchange.com/questions/136174/…
    – Pic Mickael
    Nov 2 at 15:07















up vote
1
down vote













I do not have the exact code with me right now.



But if you did not update your point of view coordinates, this should be the expected result.



Consider a real ball with 2 colors, blue and red, separated vertically.
When you are in front of it, seeing only the blue side, stroking it up will make the blue side go up and the red side appear from the bottom.
Now move behind it, seeing only the red side, and stroke it up again.
The blue face will go down and appear from the bottom.
Unity applies physics to virtual objects the same way we interact with real objects.



So you need to consider your camera position with the object orientation when you apply movements to it.
You need to apply a transformation matrix to your movement based on your camera location related to the object origin orientation.



I hope this is clear enough to put you on tracks to fix it.






share|improve this answer





















  • This sounds like what I need, thank you! I would be grateful if you could add the code to your answer so that I could test it. Also, I added a small UPD in the question description, which might also be helpful as an example.
    – Rumata
    Nov 2 at 14:56












  • And I also added a video demo as an illustration.
    – Rumata
    Nov 2 at 15:06










  • Take a look at the very good answer/explanations by DMGregory in this link gamedev.stackexchange.com/questions/136174/…
    – Pic Mickael
    Nov 2 at 15:07













up vote
1
down vote










up vote
1
down vote









I do not have the exact code with me right now.



But if you did not update your point of view coordinates, this should be the expected result.



Consider a real ball with 2 colors, blue and red, separated vertically.
When you are in front of it, seeing only the blue side, stroking it up will make the blue side go up and the red side appear from the bottom.
Now move behind it, seeing only the red side, and stroke it up again.
The blue face will go down and appear from the bottom.
Unity applies physics to virtual objects the same way we interact with real objects.



So you need to consider your camera position with the object orientation when you apply movements to it.
You need to apply a transformation matrix to your movement based on your camera location related to the object origin orientation.



I hope this is clear enough to put you on tracks to fix it.






share|improve this answer












I do not have the exact code with me right now.



But if you did not update your point of view coordinates, this should be the expected result.



Consider a real ball with 2 colors, blue and red, separated vertically.
When you are in front of it, seeing only the blue side, stroking it up will make the blue side go up and the red side appear from the bottom.
Now move behind it, seeing only the red side, and stroke it up again.
The blue face will go down and appear from the bottom.
Unity applies physics to virtual objects the same way we interact with real objects.



So you need to consider your camera position with the object orientation when you apply movements to it.
You need to apply a transformation matrix to your movement based on your camera location related to the object origin orientation.



I hope this is clear enough to put you on tracks to fix it.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 2 at 14:50









Pic Mickael

621722




621722












  • This sounds like what I need, thank you! I would be grateful if you could add the code to your answer so that I could test it. Also, I added a small UPD in the question description, which might also be helpful as an example.
    – Rumata
    Nov 2 at 14:56












  • And I also added a video demo as an illustration.
    – Rumata
    Nov 2 at 15:06










  • Take a look at the very good answer/explanations by DMGregory in this link gamedev.stackexchange.com/questions/136174/…
    – Pic Mickael
    Nov 2 at 15:07


















  • This sounds like what I need, thank you! I would be grateful if you could add the code to your answer so that I could test it. Also, I added a small UPD in the question description, which might also be helpful as an example.
    – Rumata
    Nov 2 at 14:56












  • And I also added a video demo as an illustration.
    – Rumata
    Nov 2 at 15:06










  • Take a look at the very good answer/explanations by DMGregory in this link gamedev.stackexchange.com/questions/136174/…
    – Pic Mickael
    Nov 2 at 15:07
















This sounds like what I need, thank you! I would be grateful if you could add the code to your answer so that I could test it. Also, I added a small UPD in the question description, which might also be helpful as an example.
– Rumata
Nov 2 at 14:56






This sounds like what I need, thank you! I would be grateful if you could add the code to your answer so that I could test it. Also, I added a small UPD in the question description, which might also be helpful as an example.
– Rumata
Nov 2 at 14:56














And I also added a video demo as an illustration.
– Rumata
Nov 2 at 15:06




And I also added a video demo as an illustration.
– Rumata
Nov 2 at 15:06












Take a look at the very good answer/explanations by DMGregory in this link gamedev.stackexchange.com/questions/136174/…
– Pic Mickael
Nov 2 at 15:07




Take a look at the very good answer/explanations by DMGregory in this link gamedev.stackexchange.com/questions/136174/…
– Pic Mickael
Nov 2 at 15:07










up vote
1
down vote













I think you have to somehow clamp the rotation to have the desired behaviour. I wrote a script recently to do just that. I did a little modification though.



public float rotSpeed = 30f;

float ClampAngle(float _angle, float _min, float _max)
{
if (_angle < 0f) _angle = 360 + _angle;
if (_angle > 180f) Mathf.Max(_angle, 360 + _min);
return Mathf.Min(_angle, _max);
}


USAGE:



void RotateGameObject()
{
float h = Input.GetTouch(0).deltaPosition.x * Time.deltaTime * rotSpeed*Mathf.Deg2Rad;
float v = Input.GetTouch(0).deltaPosition.y * Time.deltaTime * rotSpeed*Mathf.Deg2Rad;

Vector3 rot = transform.rotation.eulerAngles + new Vector3(-v, h, 0f);
//Change the y & z values to match your expected behaviour.
rot.x = ClampAngle(rot.x, -5f, 20f);
//Clamp rotation on the y-axis
rot.y = ClampAngle(rot.y, -20f, 20f);
transform.eulerAngles = rot;
}


See if that works and of course, try to play with the values.






share|improve this answer





















  • Thank you for an interesting example, I tried to call RotateGameObject() function OnMouseDrag(), it works in a bit strange way - the rotation of the object "jumps" to the default state several times during the finger swipe. Please let me know if I'm using it wrong. Still very interesting code, I can learn a lot from it, thanks!
    – Rumata
    Nov 2 at 16:44















up vote
1
down vote













I think you have to somehow clamp the rotation to have the desired behaviour. I wrote a script recently to do just that. I did a little modification though.



public float rotSpeed = 30f;

float ClampAngle(float _angle, float _min, float _max)
{
if (_angle < 0f) _angle = 360 + _angle;
if (_angle > 180f) Mathf.Max(_angle, 360 + _min);
return Mathf.Min(_angle, _max);
}


USAGE:



void RotateGameObject()
{
float h = Input.GetTouch(0).deltaPosition.x * Time.deltaTime * rotSpeed*Mathf.Deg2Rad;
float v = Input.GetTouch(0).deltaPosition.y * Time.deltaTime * rotSpeed*Mathf.Deg2Rad;

Vector3 rot = transform.rotation.eulerAngles + new Vector3(-v, h, 0f);
//Change the y & z values to match your expected behaviour.
rot.x = ClampAngle(rot.x, -5f, 20f);
//Clamp rotation on the y-axis
rot.y = ClampAngle(rot.y, -20f, 20f);
transform.eulerAngles = rot;
}


See if that works and of course, try to play with the values.






share|improve this answer





















  • Thank you for an interesting example, I tried to call RotateGameObject() function OnMouseDrag(), it works in a bit strange way - the rotation of the object "jumps" to the default state several times during the finger swipe. Please let me know if I'm using it wrong. Still very interesting code, I can learn a lot from it, thanks!
    – Rumata
    Nov 2 at 16:44













up vote
1
down vote










up vote
1
down vote









I think you have to somehow clamp the rotation to have the desired behaviour. I wrote a script recently to do just that. I did a little modification though.



public float rotSpeed = 30f;

float ClampAngle(float _angle, float _min, float _max)
{
if (_angle < 0f) _angle = 360 + _angle;
if (_angle > 180f) Mathf.Max(_angle, 360 + _min);
return Mathf.Min(_angle, _max);
}


USAGE:



void RotateGameObject()
{
float h = Input.GetTouch(0).deltaPosition.x * Time.deltaTime * rotSpeed*Mathf.Deg2Rad;
float v = Input.GetTouch(0).deltaPosition.y * Time.deltaTime * rotSpeed*Mathf.Deg2Rad;

Vector3 rot = transform.rotation.eulerAngles + new Vector3(-v, h, 0f);
//Change the y & z values to match your expected behaviour.
rot.x = ClampAngle(rot.x, -5f, 20f);
//Clamp rotation on the y-axis
rot.y = ClampAngle(rot.y, -20f, 20f);
transform.eulerAngles = rot;
}


See if that works and of course, try to play with the values.






share|improve this answer












I think you have to somehow clamp the rotation to have the desired behaviour. I wrote a script recently to do just that. I did a little modification though.



public float rotSpeed = 30f;

float ClampAngle(float _angle, float _min, float _max)
{
if (_angle < 0f) _angle = 360 + _angle;
if (_angle > 180f) Mathf.Max(_angle, 360 + _min);
return Mathf.Min(_angle, _max);
}


USAGE:



void RotateGameObject()
{
float h = Input.GetTouch(0).deltaPosition.x * Time.deltaTime * rotSpeed*Mathf.Deg2Rad;
float v = Input.GetTouch(0).deltaPosition.y * Time.deltaTime * rotSpeed*Mathf.Deg2Rad;

Vector3 rot = transform.rotation.eulerAngles + new Vector3(-v, h, 0f);
//Change the y & z values to match your expected behaviour.
rot.x = ClampAngle(rot.x, -5f, 20f);
//Clamp rotation on the y-axis
rot.y = ClampAngle(rot.y, -20f, 20f);
transform.eulerAngles = rot;
}


See if that works and of course, try to play with the values.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 2 at 15:08









bolkay

3478




3478












  • Thank you for an interesting example, I tried to call RotateGameObject() function OnMouseDrag(), it works in a bit strange way - the rotation of the object "jumps" to the default state several times during the finger swipe. Please let me know if I'm using it wrong. Still very interesting code, I can learn a lot from it, thanks!
    – Rumata
    Nov 2 at 16:44


















  • Thank you for an interesting example, I tried to call RotateGameObject() function OnMouseDrag(), it works in a bit strange way - the rotation of the object "jumps" to the default state several times during the finger swipe. Please let me know if I'm using it wrong. Still very interesting code, I can learn a lot from it, thanks!
    – Rumata
    Nov 2 at 16:44
















Thank you for an interesting example, I tried to call RotateGameObject() function OnMouseDrag(), it works in a bit strange way - the rotation of the object "jumps" to the default state several times during the finger swipe. Please let me know if I'm using it wrong. Still very interesting code, I can learn a lot from it, thanks!
– Rumata
Nov 2 at 16:44




Thank you for an interesting example, I tried to call RotateGameObject() function OnMouseDrag(), it works in a bit strange way - the rotation of the object "jumps" to the default state several times during the finger swipe. Please let me know if I'm using it wrong. Still very interesting code, I can learn a lot from it, thanks!
– Rumata
Nov 2 at 16:44


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53027055%2fhow-to-rotate-an-object-along-all-3-axes-with-finger-swipe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ