- Home /
How to use Transform.TransformDirection of another transform to affect a separate Transforms local rotation?
I'm trying to create a third person Shooter on the mobile platforms.
My current hierarchy is as follows:
Transform - Player
Transform - Weapon
Weapon Transform Child - Target
The player uses animator to move and the weapon transform is used to rotate the weapon via Inverse kinematics. The weapon points at the target.
The problem arises with the player changing direction, i use:
targetPoint = new Vector3(sphereTarget.transform.position.x, transform.position.y, sphereTarget.transform.position.z) - transform.position; targetRotation = Quaternion.LookRotation(targetPoint, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0f);
This has the player's transform(with the animator and collider) facing the direction of the target. I want the rotation to be relative to the players transform at all times, but this does not change, the rotation at the moment is in world space and not local space.
If i child the weapon transform to the player, i can use its local space correctly and can get the weapon to rotate with the correct direction, but doing this causes the player to spin out of control as it tries to follow the target within its own local space.
So i've come across Transform.TransformDirection and i need help in making sense of how i could use it to affect the rotation without parenting transforms.
Code i use to rotate weapon:
angle2 = Mathf.Atan2(joystickxy.x, joystickxy.y) * Mathf.Rad2Deg;
weaponTransform.localRotation = Quaternion.Euler(0, angle2, 0);
Any help is appreciated, thanks.
Your answer
Follow this Question
Related Questions
How do I make an object move on the world space rather than the local space? 1 Answer
DragRigidbody - move object in Local Space, not World Space 1 Answer
Clamping rotation doesn't work when local axis is different from global 0 Answers
Transform.rotation is setting local rotatoin 0 Answers
Idk what's wrong with my code.. 1 Answer