The question is answered, right answer was accepted
Touch position returns weird behaviours
So I am transformig the rotation of my gameObject to the touch position. I need it to look at touch position. first I tried using LookAt:
transform.LookAt(normalizedPosition);
but it resulted in very weird behaviours.
And than I tried to use eualerAngles to alter the rotation of Z axis spesifically.
transform.eulerAngles = new Vector3(transform.rotation.x, transform.rotation.y, normalizedPosition.z * 1000);
But this resulted in very weird behaviours too.
Now finally what I tried as a last effort was to use transform.rotate.
transform.Rotate(0, 0, normalizedPosition.z * 1000);
This resulted in weird behaviour too.
Here's my final code that I am using:
if (Input.touchCount > 0)
{
Debug.Log("Touched meh");
touch = Input.GetTouch(0);
Vector3 touchPosition = camera.ScreenToWorldPoint(touch.position);
Vector3 normalizedPosition = (touchPosition - transform.position).normalized;
if (transform.rotation.z != normalizedPosition.z)
{
transform.Rotate(0, 0, normalizedPosition.z * 1000);
}
}
And heres what I mean by weird behaviour.
what you want to do with?
if (transform.rotation.z != normalizedPosition.z)
{
transform.Rotate(0, 0, normalizedPosition.z * 1000);
}
second normalizedPosition means the direction vector (from 0 to 1) and rotate
and eulerAngle
use degrees (from 0 to 360) you know, one is a vector on 3d space and the other is... angles from 0 rotation ( eulerAngles
) and from current rotation ( Rotate
) LookAt probably wont work because you use another axis, different from forward (the blue on scene) as the forward of your object you can try the following:
correct your 3d object, to face the blue axis
attach a empty parent to your object, rotate the child so the blue axis of the parent is the face of the child and use
lookAt
on the parentuse the real "face" axis to force the rotation use
transform.up = normalizedPosition;
if the "face" is pointing to the green vector, or use
transform.right = normalizedPosition;
if the "face" is to the red vector
The assignment of
transform.forward = normalizedPosition;
is an elegant solution if OP's transform lines up nicely, yeah.
I dont know if your comment is sarcasm, but I think to your solution, handling it from the screen using atangent is better than override the value of an axis, I was editing the comment and I didn't saw it before.
Answer by TreyH · Feb 25, 2019 at 05:57 PM
Your units aren't lining up to anything intelligible, so code will have pretty much random outcomes. Transform.Rotate takes units in degrees, but you are actually supplying meters with the z-component of a Vector3.
But, you had the right idea, though. Normalizing the offset is your first step, turning that into a relative polar angle is the second:
void Update()
{
if (Input.touchCount > 0)
{
// It's more intuitive to just get the object's appearance on screen. Casting
// a screen point into the world is a little murky as you aren't always sure
// of where it will end up.
//
Vector3 mouse = Input.GetTouch(0).position;
Vector3 thisOnScreen = Camera.main.WorldToScreenPoint(this.transform.position);
// Get the noramlized difference between our mouse and object's screen representation
Vector3 relative = (mouse - thisOnScreen).normalized;
// Get a degree representation around the origin for that difference
float theta = Mathf.Atan2(relative.y, relative.x);
float thetaDegrees = theta * Mathf.Rad2Deg;
// This will change depending on your situation. In my case, it was just an arrow
// point right, but you might need to add a multiple of 90 here to correct your heading.
this.transform.rotation = Quaternion.Euler(Vector3.forward * thetaDegrees);
}
}
giving you something like:
Behavior looks a lot more normal compared to my approach but object still rotates irrelevant to my finger's touch input position. I am trying to make it look towards my input position. I am not familiar with how Quaternion actually works. I looked it up to try to implement it better in the code but it contains mathematical calculations beyond my knowledge...
Where are you putting the script in your object hierarchy?
Your object also moves when you apply the first touch, making me think your script is on a parent object or something.
Go into a blank scene and just make a single object with a SpriteRenderer. Attach a script with the above block as its only code and see what happens.