Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kieran · Dec 08, 2009 at 04:21 PM · movementcharactermouse

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;
}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image duck ♦♦ · Jan 12, 2010 at 11:05 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by dkoontz · Dec 14, 2009 at 11:59 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Move object towards mouse in full 3D space 2 Answers

Enemy not moving towards Player 0 Answers

Hiccup between player movement and mouse control after game start. 0 Answers

Move an object from point A to B? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges