|
Alex Bogartz, Sr. Member
Posted: 13 October 2011 11:17 PM Total Posts: 216
Ok, it took me a while to get this working, but I thought I’d share a simple script a have your camera follow the character with easing from behind him. I’ll just post the basic function, and you can fill in the constants.
private function moveCamera() { if (!cameraTarget) return; var desiredDist:Number = 700; var radians:Number; radians = getRadians(cameraTarget.rotationY + 180); var targetX:Number = cameraTarget.position.x + Math.sin(radians) * radius; if (cameraTarget.forwardVector.z <0) { radians = getRadians(cameraTarget.rotationY ); } var targetZ:Number = cameraTarget.position.z + Math.cos(radians) * radius; var dx:Number = targetX - camera.position.x; var dy:Number = (cameraTarget.position.y + 1000) - camera.position.y; var dz:Number = (targetZ) - camera.position.z; var vx:Number = dx * CAMERA_SPEED * 2;//this is set to .05 var vy:Number = dy * CAMERA_SPEED; var vz:Number = dz * CAMERA_SPEED * 2; if (vx > MAX_CAMERA_SPEED || vx < -MAX_CAMERA_SPEED) { vx = vx < 1 ? -MAX_CAMERA_SPEED : MAX_CAMERA_SPEED; //max speed is 40 } if (vy > MAX_CAMERA_SPEED || vy < -MAX_CAMERA_SPEED) { vy = vy < 1 ? -MAX_CAMERA_SPEED : MAX_CAMERA_SPEED; } if (vz > MAX_CAMERA_SPEED || vz < -MAX_CAMERA_SPEED) { vz = vz < 1 ? -MAX_CAMERA_SPEED : MAX_CAMERA_SPEED; } camera.position = new Vector3D(camera.position.x + vx, camera.position.y + vy, camera.position.z + vz) camera.lookAt(cameraTarget.position); }
Let me know if this helps anyone!
|
|
|
3dNewb, Sr. Member
Posted: 27 October 2011 10:03 AM Total Posts: 105
[ # 3 ]
What is the “LookAtController” I cannot seem to find any reference to it in the API documentations.
|
Alex Bogartz, Sr. Member
Posted: 27 October 2011 03:59 PM Total Posts: 216
[ # 4 ]
|
3dNewb, Sr. Member
Posted: 27 October 2011 05:21 PM Total Posts: 105
[ # 5 ]
Ah I see thanks for that link. Can I ask you maybe to help me understand this a bit? The Controller package is not in the Away3D that I downloaded from the Downloads section, so I must have been added later on. How do I get up to speed with new packages and classes (like these Controller classes)? Or should I just check out the git more often and look for changes?
|
Alex Bogartz, Sr. Member
Posted: 27 October 2011 05:26 PM Total Posts: 216
[ # 6 ]
Yeah, that’s what I do. I notice ... to my annoyance ... that the classes are refactored quite often. So it’s best to grab that github repo or at least subscribe to the RSS feed to track major changes.
|
3dNewb, Sr. Member
Posted: 27 October 2011 05:36 PM Total Posts: 105
[ # 7 ]
I’ll try and do that then, thanks
|
Hector, Sr. Member
Posted: 03 November 2011 11:36 AM Total Posts: 137
[ # 8 ]
Thanks for sharing Alex, I probably use it in the future.
|
cosmicwiz, Newbie
Posted: 20 February 2012 06:57 AM Total Posts: 6
[ # 9 ]
Hi, I tried to use this code for my own third person view but somehow I always end up with my scenery upside down. Please note that my scenery has Z-axis pointing up (so previously I used to do camera.rotationX = 90 for the plain FPS view). I can’t seem to figure out how to rotate the camera so that I get the z-axis back pointing up (not down).
I am total newbie and this 3d stuff is driving me nuts. Is there a good tutorial I can follow to understand how lookAt is supposed to behave?
Thanks!
|
Alex Bogartz, Sr. Member
Posted: 20 February 2012 09:34 PM Total Posts: 216
[ # 10 ]
If your models are loading in rotated, I think you may want to rotate them all correctly as soon as they load or in your 3D software. For the lookAt function, I think it just rotates the camera to face whatever Vector3D you specify. So if your object’s position is not in the center of the object (if it is offset in your model) this will throw things off.
I am using Blender, and I am very careful about applying all transforms and centering things before I export and this seems to solve a lot of problems.
|
cosmicwiz, Newbie
Posted: 21 February 2012 03:12 AM Total Posts: 6
[ # 11 ]
Thanks Alex. After some debugging, I replaced “super.update()” with “targetObject.lookAt(lookAtObject.position, Vector3D.Z_AXIS);” in the CharacterFollowController’s update function and now things work like a charm
|
Alex Bogartz, Sr. Member
Posted: 21 February 2012 03:59 PM Total Posts: 216
[ # 12 ]
Nice! When you get something going, please post a link so I can see what you did with it!
|
Pagsli, Newbie
Posted: 07 November 2013 09:54 AM Total Posts: 4
[ # 13 ]
Hi!
Unfortunatly this extension doesn’t work with new Away version.
There is a problem in line 34:
override public function update():void
I’ve fixed with:
override public function update(interpolate:Boolean = false):void
But the Controller doesn’t work yet! : (
|