- Home /
Question by
mike29willems · Mar 26, 2019 at 02:20 PM ·
transformvector3alignment
how to transform vector3 input relative to the surface?
So I have a joystick that gives me a Vector value between -1 and 1(0.7, 0f, -0.9). I want my player to be able to move in whatever direction the joystick vector gives me relative to the surface. So when hes walking on a mountain and facing the top of that mountain I want to add force to that direction (his local forward). This way when gravity is set to -(hit.normal) he can walk on walls etc. How do I achieve this?
private void update
{ h = Joystick.Horizontal;
v = Joystick.Vertical;
}
private void Fixedupdate
{
movement.Set(h, 0f, v);
movement = movement * speed * Time.deltaTime;
playerRig.MovePosition(transform.position + movement);
}
Comment
Answer by WarmedxMints · Mar 26, 2019 at 02:30 PM
You can just get the transforms forward and right vectors then multiply them by the inputs.
private Rigidbody _myRigidbody;
private Vector3 _direction;
private void Start()
{
_myRigidbody = GetComponent<Rigidbody>();
}
private void Update()
{
var horizontalInput = Input.GetAxis("Horizontal");
var vecticalInput = Input.GetAxis("Vertical");
_direction = (transform.forward * vecticalInput) + (transform.right * horizontalInput);
}
private void FixedUpdate()
{
_myRigidbody.AddForce(_direction);
}