x


Maximum speed mouse follow without rotation

Hi, I'm just starting out and I'm trying to get a character to follow the mouse which I've seen a lot of help for. My problem is that I need the character not to rotate toward the mouse, they need to just move to it. I also want to cap the speed that they can follow the mouse. Smooth follow was the closest I came so far, but it slows me down too much as I approach the mouse.

So to recap my main goals are:

  • Follow the mouse
  • Don't rotate
  • Move at a constant speed
  • Don't go faster than a maximum speed

Here's the closest I have so far... most of this was put together from other answers on this site:

var target : Transform;
var smoothTime = 0.3;

private var thisTransform : Transform;

function Start()
{
       thisTransform = transform;
}

function Update () {

    var playerPlane = new Plane(Vector3.up, transform.position);
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hitdist = 0.0;

    if (playerPlane.Raycast (ray, hitdist)) {

        var targetPoint = ray.GetPoint(hitdist);
    }

    thisTransform.position.x = Mathf.Lerp( thisTransform.position.x, targetPoint.x, Time.deltaTime * smoothTime);
    Debug.Log(thisTransform.position.x);
    thisTransform.position.z = Mathf.Lerp( thisTransform.position.z, targetPoint.z, Time.deltaTime * smoothTime);
    Debug.Log(thisTransform.position.z);
}
more ▼

asked Jan 09 '11 at 06:32 PM

Christopher Ellis gravatar image

Christopher Ellis
2 3 3 3

I have just found out that this method of movement means that it ignores collisions. So I need to add...

  • Must allow the object to react properly to collisions (ex. Don't move through a wall)
Jan 09 '11 at 08:04 PM Christopher Ellis
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2174
x986
x358
x316
x34

asked: Jan 09 '11 at 06:32 PM

Seen: 897 times

Last Updated: Jan 09 '11 at 06:32 PM