- Home /
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.
Your answer
Follow this Question
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