x


Mouse Follow Controller

Hi all! I'm making a small independent game where I want less buttons and controls than your average game,using only the mouse and space button,but twice the Fun! :) But I have a problem. The player will be a simple ball in 3D space with a Rigidbody(3/4 top down veiw game), and the ball must follow the mouse within a certain distance. I might need variables to experiment. Usually, I write my own scripts, because I have some clue on how to start. But this one is no exception. I have absolutely no idea. If you can provide a script for me, that be awesome. Thanks!

more ▼

asked Aug 06, 2011 at 07:12 PM

Jayland gravatar image

Jayland
21 17 14 16

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

BTW, I don't want it to be clicking, Just follow the mouse without clicking.

more ▼

answered Aug 06, 2011 at 07:22 PM

Jayland gravatar image

Jayland
21 17 14 16

(comments are locked)
10|3000 characters needed characters left

You must cast a ray at Update and translate the object to the hit point - if any. This script was intended to run in the XZ plane, and the object must have a rigidbody with Use Gravity checked.

var speed: float = 2; var maxSpeed: float = 20; private var dest: Vector3;

function Start(){ dest = transform.position; }

function Update(){ var ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit: RaycastHit; if (Physics.Raycast(ray, hit)){ // if mouse pointer hit dest = hit.point; // some update destination } var mov = dest - transform.position; // find direction to move mov.y = 0; // zero y to do not interfere with gravity mov = mov speed; // apply speed if (mov.magnitude > maxSpeed){ // clamp speed to maxSpeed mov = mov.normalized maxSpeed; } // move in horizontal direction transform.Translate(mov * Time.deltaTime, Space.World); }

more ▼

answered Aug 06, 2011 at 10:59 PM

aldonaletto gravatar image

aldonaletto
58.2k 34 106 316

(comments are locked)
10|3000 characters needed characters left
Your answer
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:

x1480
x722
x515
x293
x25

asked: Aug 06, 2011 at 07:12 PM

Seen: 2974 times

Last Updated: Aug 06, 2011 at 10:59 PM