Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 hamburger891 · Nov 26, 2014 at 07:21 AM · rotationcharacter controllercursormouse cursor

Help with rotation!

Hello. I have a problem. My character has a character controller applied on him. I have a script to make the character move and rotate. It rotates when i slide the mouse left and right. But i want to make that the character moves when you have the mouse cursor in front of him, that it turns to the direction the cursor is pointing. Help me out please! The script:

pragma strict

var rotationSpeed : float = 10; var walkSpeed : float = 7; var gravity : float = -50; private var yRot : float; var body : Transform;

function Start () {

}

function Update () { var Controller : CharacterController = GetComponent(CharacterController); var vertical : Vector3 = transform.TransformDirection(Vector3.forward); var horizontal : Vector3 = transform.TransformDirection(Vector3.right); var height : Vector3 = transform.TransformDirection(Vector3.up);

 if(Input.GetKeyDown("space")){
     animation.CrossFade("Jump", 0.1);
     Jump();
 }else{
     animation.CrossFade("Idle", 0.1);
 }
 
 if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")){
     animation.CrossFade("Walk", 0.1);
     Controller.Move((vertical * (walkSpeed * Input.GetAxis("Vertical"))) * Time.deltaTime);
     Controller.Move((horizontal * (walkSpeed * Input.GetAxis("Horizontal"))) * Time.deltaTime);
 }else{
     animation.CrossFade("Idle", 0.1);
 }
 
 if(Input.GetAxis("Mouse X")){
     yRot += 10 * Input.GetAxis("Mouse X");
 }
 transform.rotation = Quaternion.Euler(0, yRot, 0);
 
 Controller.Move(height * gravity * Time.deltaTime);    

}

 function Jump(){
     gravity = 10;
     yield WaitForSeconds(0.2);
     gravity = -20;
     yield WaitForSeconds(0.3);
     gravity = -30;
     yield WaitForSeconds(0.3);
     gravity = -40;
     yield WaitForSeconds(0.3);
     gravity = -50;
     yield WaitForSeconds(0.3);
     gravity = -60;
 }
Comment
Add comment · Show 2
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 Salmjak · Nov 26, 2014 at 09:05 AM 1
Share

I think you should use the pythagorean theorem to get the angle between transform.position.x and $$anonymous$$ouse.x(?) and rotate accordingly.

Not entirely sure what you want to do... You want the player to rotate towards the mouse and when it has rotated you want it to start moving towards the mouse? But if so, why do you have "if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal"))"?

EDIT: With pythagorean theorem I ofcourse mean trigonometry. First of all you would have to set up 4 different "If"-statements to deter$$anonymous$$e which quadrant the position of the mouse is in relative to the player. After that you would have to use e.g. arctan to get the angle between the player and the mouse and after that rotate accordingly.

avatar image richyrich · Nov 26, 2014 at 12:32 PM 1
Share

For Vector3, consider these functions:

http://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html

http://docs.unity3d.com/ScriptReference/Vector3.$$anonymous$$oveTowards.html

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

character controller rotation won't accept script values 2 Answers

Is there a way to get the character controller to rotate? 0 Answers

cursor appear as a white quare when using hardware cursor 1 Answer

Character Controller strange collision bug 2 Answers

How to Prevent Character Bouncing 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