I am trying to make an a character controller that allows the player to press a location on the trackpad and that would be the direction that they are going. This is what i have so far and it works perfectly with one problem:
Vector2 touchpad = (touchPadAction.GetAxis(RightHandSource));
Vector3 direction = new Vector3( PlayerSpeed * Time.deltaTime * touchpad.x, 0,PlayerSpeed *
Time.deltaTime * touchpad.y);
direction = transform.rotation * direction;
rb.velocity = direction ;
However, i want this to be relative to the rotation of an object. Previously i had used this:
else if (touchpad.y < -0.4f)
{
// move backwards
Vector3 forward = CurrentMovementType.transform.forward;
forward.y = 0;
forward.Normalize();
rb.velocity = ((forward * PlayerSpeed * Time.deltaTime) * -1);
}
else if (touchpad.x > 0.4f)
{
//move left
Vector3 forward = CurrentMovementType.transform.right;
forward.y = 0;
forward.Normalize();
rb.velocity = ((forward * PlayerSpeed * Time.deltaTime));
}
else if (touchpad.x < -0.4f)
{
//move right
Vector3 forward = CurrentMovementType.transform.right;
forward.y = 0;
forward.Normalize();
rb.velocity = ((forward * PlayerSpeed * Time.deltaTime) * -1);
(The first IF is missing, do not worry about that as i had reused it somewhere and cannot find it, but it follows the same format)
So the this code is relative to the “CurrentMovementType” which is one of 3 objects: left hand , right hand or face direction. It works, but it does not allow movement in all directions, only up down right and left. Not top left, top right, etc.
I like the second code allot more and i feel like i am just missing 1 piece to make it work relative to another object as currently it is just towards the direction of the Player gameobject.