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

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Simple Character Movement 1 Answer

Orientation of character controller relative to ground normal? 0 Answers

How to make an object orient to the mouse direction 0 Answers

How to add platform motion to player controller without parenting? - Mostly working code 1 Answer

How to keep playing from falling off edge with placeable tiles 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