x


c# scripting navigation problems

hi, i am trying to edit the animation so when the user click's the 'fire1' button it move the chracter controller in the direction the mouse is pointing. i have got the user to move when they press the mouse button but it will only go in one direction. Here my script if you can help thank you:

else if (Input.GetButton("Fire1"))
{
    CharacterController controller = GetComponent<CharacterController>();
    moveDirection = new Vector3(-5, 0, 0);
    controller.Move(Vector3.forward * Time.deltaTime);
    rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    rotationX = ClampAngle (rotationX, minimumX, maximumX);
    Quaternion xQuaternion = Quaternion.AngleAxis (rotationX,Vector3.up);
    transform.localRotation = originalRotation * xQuaternion;
}
more ▼

asked Dec 08 '09 at 04:21 PM

kieran gravatar image

kieran
11 1 1 1

Please learn how to correctly format your code snippets (fixed for you this time).

Jan 12 '10 at 11:05 AM duck ♦♦
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The reason your character is always moving in the same direction is that you are always moving your character controller along its forward vector. To make the character move in the direction of the mouse, you would first need to calculate the "position" of the mouse in the game world and then calculate the angle between the character's current heading, and then rotate the character by that amount (or use Transform.LookAt if you don't care about a smooth rotation). To get the position of the mouse in your world, well, that depends a lot on what your game world is like. Should the mouse click's position be the top-most object under the mouse? Only the ground? Some combination? Without knowing more I can only offer a possible solution:

During the update of one of your game objects check to see if the mouse is clicked. If so, shoot a ray into the scene and set the layer mask so that you only collide with a certain layer that contains everything you want to be clickable for the purposes of your character's movement. You can shoot a ray into the scene using:

 int layer = 1 << LayerMask.NameToLayer("name of your collision layer");
 RaycastHit hitInfo = new RaycastHit();
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 if(Physics.Raycast(ray, out hitInfo, Mathf.Infinity, layer)) {
     // use hitInfo here to determine where the clicked point 
     // was and orient your character towards it
 }

Again, it's a fairly complex thing you're asking about but hopefully this is at least a start for you.

more ▼

answered Dec 14 '09 at 11:59 PM

dkoontz gravatar image

dkoontz
185 7 8 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5268
x1417
x1079
x1001

asked: Dec 08 '09 at 04:21 PM

Seen: 2407 times

Last Updated: Jan 12 '10 at 10:51 AM