Im quite new to 3D programming. I have modified a script for manouvering a gameObject with third person controller component. Now i need to make it go to a certain position on the map (for example via clicking the mouse).
This is what i got `void UpdateSmoothedMovementDirection ()
{
Transform cameraTransform = Camera.main.transform;
bool grounded = IsGrounded ();
// Forward vector relative to the camera along the x-z plane
Vector3 forward = cameraTransform.TransformDirection (Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
Vector3 right = new Vector3 (forward.z, 0, -forward.x);
float v = Input.GetAxisRaw ("Vertical");
float h = Input.GetAxisRaw ("Horizontal");
// Are we moving backwards or looking backwards
if (v < -0.2f)
movingBack = true;
else
movingBack = false;
bool wasMoving = isMoving;
isMoving = Mathf.Abs (h) > 0.1f || Mathf.Abs (v) > 0.1f;
// Target direction relative to the camera
Vector3 targetDirection = h * right + v * forward;
// Grounded controls
if (grounded) {
// // Lock camera for short period when transitioning moving & standing still
// lockCameraTimer += Time.deltaTime;
// if (isMoving != wasMoving)
// lockCameraTimer = 0.0f;
// We store speed and direction seperately,
// so that when the character stands still we still have a valid forward direction
// moveDirection is always normalized, and we only update it if there is user input.
if (targetDirection != Vector3.zero) {
// If we are really slow, just snap to the target direction
if (moveSpeed < walkSpeed * 0.9f && grounded) {
// moveDirection = targetDirection.normalized;
}
// smoothly turn towards it
moveDirection = Vector3.RotateTowards (moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
}
// Smooth the speed based on the current target direction
float curSmooth = speedSmoothing * Time.deltaTime;
// Choose target speed
//* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
float targetSpeed = Mathf.Min (targetDirection.magnitude, 1.0f);
// Pick speed modifier
if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) {
//WHEN RUNNING
targetSpeed *= runSpeed;
} else {
//WHEN WALKING
targetSpeed *= walkSpeed;
}
moveSpeed = Mathf.Lerp (moveSpeed, targetSpeed, curSmooth);
}
// In air controls
else {
// Lock camera while in air
if (jumping)
lockCameraTimer = 0.0f;
if (isMoving)
inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
}
// Calculate actual motion
Vector3 movement = moveDirection * moveSpeed + new Vector3 (0, verticalSpeed, 0) + inAirVelocity;
movement *= Time.deltaTime;
// Move the controller
CharacterController controller = GetComponent<CharacterController> ();
collisionFlags = controller.Move (movement);
// Set rotation to the move direction
if (IsGrounded ()) {
transform.rotation = Quaternion.LookRotation (moveDirection);
} else {
Vector3 xzMove = movement;
xzMove.y = 0;
if (xzMove.sqrMagnitude > 0.001f) {
transform.rotation = Quaternion.LookRotation (xzMove);
}
}
}
`
Oh i dont want to reposition it. I want it to travel.
– Lautaro-Arino