Dec
4
2011

Follow / Push object

Follow / push object around.

Might be useful for something..??

Webplayer:
http://unitycoder.com/upload/demos/mFollow1/

Original source:
http://processing.org/learning/topics/follow1.html

Download webplayer package: (unity js)
(coming soon..)

Source: (.js)

// mFollow ported to Unity : mgear : http://unitycoder.com/blog
// attach this script to camera, then assign some object(with linerenderer) as "object"

// original : http://processing.org/learning/topics/follow1.html
// All Examples Written by Casey Reas and Ben Fry
// unless otherwise stated.

private var x:float = 0;
private var y:float = 0;
private var angle1:float = 0.0;
public var segLength:int = 2;
public var object : Transform;
private var mousePos:Vector3;
// mainloop
function Update ()
{
// get mouse position
mousePos = Input.mousePosition;
mousePos.z = 10.0;       // fixed distance from camera
// convert mousepos into world pos
var MouseWorldPos = Camera.main.ScreenToWorldPoint(mousePos);
// not sure whats happening here..
var dx:float = MouseWorldPos.x-x;
var dy:float = MouseWorldPos.y-y;
var angle1:float = Mathf.Atan2(dy, dx);
x = MouseWorldPosOrig.x - (Mathf.Cos(angle1) * segLength);
y = MouseWorldPosOrig.y - (Mathf.Sin(angle1) * segLength);
// set object pos
object.transform.position = Vector3(x,y,10);
// draw line from object to mouse
var lineRenderer : LineRenderer = object.GetComponent(LineRenderer);
lineRenderer.SetPosition(0,MouseWorldPos);
lineRenderer.SetPosition(1,object.transform.position);
}

Related Posts

About the Author:

.fi

1 Comment + Add Comment

  • x, y represents the position of object, then draw a line from mouseWorldPos to object

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch Instagram

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.