• 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 Yanneyanen · Feb 26, 2012 at 10:51 PM · movementcharacter controllerfollowcursorforward

Moving forward towards the cursor with LookAtMouse?

I've been having some trouble getting my character controller to work the way I want it to. Right now I'm just using the basic Character Controller ( which I think I got from the wiki, but I'm not sure since I couldn't find it again, so I'll post it here:)

 /// This script moves the character controller forward
 /// and sideways based on the arrow keys.
 /// It also jumps when pressing space.
 /// Make sure to attach a character controller to the same game object.
 /// It is recommended that you make only one call to Move or SimpleMove per frame.
 
 var speed : float = 6.0;
 var jumpSpeed : float = 8.0;
 var gravity : float = 20.0;
 
 private var moveDirection : Vector3 = Vector3.zero;
 
 function Update() {
 var controller : CharacterController = GetComponent(CharacterController);
 if (controller.isGrounded) {
 
 // We are grounded, so recalculate
 // move direction directly from axes
 moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
 Input.GetAxis("Vertical"));
 moveDirection = transform.TransformDirection(moveDirection);
 moveDirection *= speed;
 
 if (Input.GetButton ("Jump")) {
 moveDirection.y = jumpSpeed;
 }
 }
 
 // Apply gravity
 moveDirection.y -= gravity * Time.deltaTime;
 
 // Move the controller
 controller.Move(moveDirection * Time.deltaTime);
 }

and LookAtMouse (with Input.GetMouseButton(0), so the character turns when clicking:)

 // LookAtMouse will cause an object to rotate toward the cursor, along the y axis.
 //
 // To use, drop on an object that should always look toward the mouse cursor.
 // Change the speed value to alter how quickly the object rotates toward the mouse.
 
 // speed is the rate at which the object will rotate
 var speed = 8.0;
 
 function Update () {
     if (Input.GetMouseButton(0))
     // Generate a plane that intersects the transform's position with an upwards normal.
     var playerPlane = new Plane(Vector3.up, transform.position);
     
     // Generate a ray from the cursor position
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     
     // Determine the point where the cursor ray intersects the plane.
     // This will be the point that the object must look towards to be looking at the mouse.
     // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
     //   then find the point along that ray that meets that distance.  This will be the point
     //   to look at.
     var hitdist = 0.0;
     // If the ray is parallel to the plane, Raycast will return false.
     if (playerPlane.Raycast (ray, hitdist)) {
         // Get the point along the ray that hits the calculated distance.
         var targetPoint = ray.GetPoint(hitdist);
         
         // Determine the target rotation.  This is the rotation if the transform looks at the target point.
         var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
         
         // Smoothly rotate towards the target point.
         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
     }
 }

The problem is that these are independent of each other (I can move with wasd, and rotate the character with mouse). What I'd want to do is to get the character to move forward (to the direction of the cursor, the direction he's facing) when clicking the mouse.

Is there an easy (enough) way to do this? Just adding something to the LookAtMouse perhaps? I'm not good at programming, so since I've tried various ways and can't seem to get it right, I'd appreciate a ready-to-use script. Any help even pointing me to the right direction is appreciated, of course.

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Player Controller moves automatically forward. cant stop. 1 Answer

Change forward control on mouse rotation 0 Answers

With a gameobject cursor, what would be the best way to get it to follow the real cursor, but locked to a grid? 1 Answer

Rotate a character controller in the direction of input axis. 2 Answers

"Chasing" object around a sphere 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges