- Home /
Scripting BUG: target becomes bigger when getting nearer
I try to brief on this one because I was kinda this problem for a little while :< I have a zombie prefab (animations included), 2 scripts - Look.JS and Chase.JS, and a First-Person Character controller. Here's my code for the script:
Look.JS:
var target : Transform;
function Update() {
transform.LookAt(target);
}
And Chase.JS:
var target : Transform;
var speed : int;
function Update() {
animation.Play('walk');
transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * speed);
}
The script works and the zombie followed me while playing the 'walk' animation. But there's seems to be some sort of code error or something that whenever the zombie gets nearer to me, he became more bigger and bigger o.O Except from that the code works perfect. Can you help me how to fix this? I need this for my school project and deadline is 2 days from now and I'm kinda stuck here :( Thank you! :)
Nothing you posted will affect the size of anything. The model will appear to get bigger as it approaches your perspective camera. If there is nothing to stop it from doing so, such as a collider or a coded response to proximity, it will attempt to occupy the same position as the target, which may account for "getting bigger and bigger".
If you believe this is what's happening - that the object simply gets too close to the camera - you have several options.
You might simply avoid seeking if within a certain distance:
Vector3 difference = target.position - transform.position;
float magnitude = difference.magnitude;
if (magnitude > 0.5f) {
animation.Play('walk');
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target.position, Time.deltaTime * speed);
}
One thing to try would be to reduce the field of view and then pull the camera back. This doesn't fix the problem, but it might reduce the distortion associated with having an object close. Or if you really don't want things to get bigger, change your camera to orthographic.
This isn't a bug, its just how things work. This goes for real life, too.
Put your hand out at arms length, then move it towards your face. See how it takes up more of your view, the closer it gets?
Science.
Answer by Baste · Nov 12, 2014 at 12:06 AM
If it's actually a perspective thing where the thing appears larger because you're closer to it, just set your camera to orthographic instead of perspective.
Grab your main camera object in the scene, select the Projection drop-down, and select 'Ortographic'.
Please post a video or screenshot showcasing the issue so we can understand. Your description isn't reinforced by enough Unity knowledge and jargon to be informative.
Here's the video of it https://www.youtube.com/watch?v=E6UiJX66noI
I do not see the model getting bigger in this video. I see the model getting closer to the camera. It thus appears bigger. As eloquently stated by @Habitablaba,
"Put your hand out at arms length, then move it towards your face. See how it takes up more of your view, the closer it gets? Science."
Answer by dkreigns · Nov 14, 2014 at 12:56 PM
Ok I'll post another vid. I'll create a 2 of them now,, trust me he's really getting bigger -_- I'm not stupid