- Home /
Direction between two point - add unit distance to player
i have a game which have two type of control one is joy stick and another is player will move towed your touch
// FOR JOYSTICK
Vector3 headingPosition = joystickpad.position - joystickController.position;
float distance= headingPosition.magnitude;
Vector3 directtion = headingPosition / distance;
additionValue = directtion * speed;
player.transform.position -= additionValue;
//FOR PLAYER FOLLOW TOUCH
Vector3 headingPosition = player.transform.position - mousePosition;
float distance= headingPosition.magnitude;
Vector3 directtion = headingPosition / distance;
additionValue = directtion * speed;
player.transform.position -= additionValue;
my player moves in right direction but the speed of both is difference. how can i make both the speed equal.
i have also tried with normalized but its not working
at last i am adding that position to player position... this code is written in update..so when touch is detected distance will be calculated and additional value should be added to player position
Answer by TruffelsAndOranges · Jul 13, 2015 at 02:35 PM
Try this:
// FOR JOYSTICK
Vector3 headingPosition = joystickpad.position - joystickController.position;
float distance= headingPosition.magnitude;
Vector3 directtion = headingPosition / distance;
additionValue = directtion.normalized * speed;
player.transform.position -= additionValue * Time.deltaTime;
//FOR PLAYER FOLLOW TOUCH
Vector3 headingPosition = player.transform.position - mousePosition;
float distance= headingPosition.magnitude;
Vector3 directtion = headingPosition / distance;
additionValue = directtion.normalized * speed;
player.transform.position -= additionValue * Time.deltaTime;
Thanks for replying. still there is huge difference between speed player movement by joystick and playermovement of touch touch is too fast than joystick, direction works fine
I think this is because difference between touch and player is bigger then difference between joystick controller and joystick centre. any help?
Since the direction is normalized there should be no difference in speed. It seems as if you either have a rigidbody that you are using wrong or that you have another script interfering with the movement.
You should place your transformation/physics code in FixedUpdate() and NOT Update().