- Home /
how do I sync movement with mouselook
I'm making a mobile game. And when I walk and lookaround everythings is ok but if I turn 180 degrees the movement is not synced so when I go forward, I go backwards! can someone please help me out with this? :)
I've a capsule that the FPSwalker script is attatched to and a camera that is a child of it with the touchlook script!
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
var joy = VCAnalogJoystickBase.GetInstance("moveStick");
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(joy.AxisX, 0, joy.AxisY);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
and heres the touchlook (this one is from unify wiki in C#)
if (Input.touches.Length > 0)
{
if (Input.touches[0].phase == TouchPhase.Moved)
{
Vector2 delta = Input.touches[0].deltaPosition;
float rotationZ = delta.x * sensitivityX * Time.deltaTime;
rotationZ = invertX ? rotationZ : rotationZ * -1;
float rotationX = delta.y * sensitivityY * Time.deltaTime;
rotationX = invertY ? rotationX : rotationX * -1;
transform.localEulerAngles += new Vector3(rotationX, rotationZ, 0);
}
}
thanks :)
Thanks for posting, how can i use this for my camera. I want to use just my finger to drag on the screen to look around. Thanks
Answer by skoandi · Aug 30, 2012 at 08:51 AM
Hahah I just woke up and solved it :D change this: `moveDirection = transform.TransformDirection(moveDirection);` to this: `moveDirection = cam.transform.TransformDirection(moveDirection);`
also add `var cam : GameObject;` and add the camera to it! :)
so now it's using cameras Transform direction! :)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can I change movement from Unity 3d into Unity 3D iOS 2 Answers
iPhone Resolution Screen Switch 0 Answers
Mini Mac - Can this Compile Unity? 2 Answers
Unity modules for iOS? 1 Answer