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 JeffRBake · Jun 18, 2013 at 08:41 PM · movecharacter controllermouse look

Character Controller follow mouse

I searched for a while and found nothing that worked. I am trying to get my character controller to follow the mouse when the right mouse button is clicked. I got it working using trasnsform.position and Vector3.lerp, but am having some issue converting it to the character controller. I have tried a number of things, but none seem to work.

 Vector3 targetPoint = ray.GetPoint(hitdist);
 if(Input.GetMouseButton(1)){
                 //transform.position = Vector3.Lerp (transform.position, targetPoint, Time.deltaTime * 5);
                 CharacterController controller = GetComponent<CharacterController>();
                 //moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
                 moveDirection = new Vector3(targetPoint.x,0,0);
                 moveDirection = transform.TransformDirection(moveDirection);
                 //Multiply it by speed.
                     moveDirection *= speed;
                 //Applying gravity to the controller
                     moveDirection.y -= gravity * Time.deltaTime;
                 //Making the character move
                 controller.Move(moveDirection * Time.deltaTime);
             }

Thanks for your help!

Comment
Add comment
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

2 Replies

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

Answer by JeffRBake · Jun 18, 2013 at 11:54 PM

Never mind, solved my own question. I simple need to use

 moveDirection = new Vector3(0,0,SomeSpeed);

where SomeSpeed is a float. Thanks for the help though!

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
avatar image
0

Answer by Seizure · Jun 18, 2013 at 09:00 PM

This is a variation of the mouse orbit that I use in my game, just have another script that enables this one when you release the right mousekey

 public GameObject target;
     private Quaternion myRotation;
     public float distance;
     public float xSpeed;
     public float ySpeed;
     public int yMinLimit;
     public int yMaxLimit;
     private float x;
     private float y;
     private Vector3 angles;
     
 
     // Use this for initialization
     void Start () {
 
             distance = 10.0f;
             
             xSpeed = 250.0f;
             ySpeed = 120.0f;
             
             yMinLimit = -20;
             yMaxLimit = 80;
             
             x = 0.0f;
             y = 0.0f;
             angles = transform.eulerAngles;
             x = angles.y;
             y = angles.x;
                 
     
     }
     
     // Update is called once per frame
     void Update () {
         
             xSpeed = 12.5f;
             ySpeed = 6.0f;
         
             if (target) 
             {
                 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                 y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
                 y = ClampAngle(y, yMinLimit, yMaxLimit);
                 myRotation = Quaternion.Euler(y, x, 0);                
                 transform.rotation = myRotation;
             }
                 
 }
     float ClampAngle (float angle, float min, float max) 
     {
         if (angle < -360)
         {
             angle += 360;
         }
         if (angle > 360)
         {
             angle -= 360;
         }
         
         return Mathf.Clamp (angle, min, max);
     }
Comment
Add comment · Show 1 · 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
avatar image JeffRBake · Jun 18, 2013 at 11:48 PM 0
Share

But how does this fair when you introduce uneven terrain? I need to use character controller because it prevents unwanted clipping.

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

move character controller smoothly to the object 1 Answer

Character controller wont move 0 Answers

i can't move anything , and nothing in Assest > Import Package any1 help me 0 Answers

Align move direction to a child object's facing direction 0 Answers

First-Person Controller with Head Bobbing 0 Answers


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