- Home /
Jumpy object movement and camera movement
Hi,
I am a beginner at Unity and am currently trying to create a simple platformer that works in only the x and y axis. I have my camera following the player object in the scene (just a simple capsule at the moment with a character controller attached). When I move the player object (the capsule) it has quite visibly jarring stutter on screen, this also affect the camera making it a little jumpy too. Here is the code
Player.js
var health = 100;
var mana = 100;
var moveSpeed = 5;
var jumpSpeed = 10;
var gravity = 20.0;
private var moveDir : Vector3 = Vector3.zero;
function Start ()
{
}
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
// calculate movement on the x-axis
moveDir.x = Input.GetAxis("Horizontal") * moveSpeed;
moveDir = transform.TransformDirection(moveDir);
// set the jumpHeight when the Jump key is pressed
if(controller.isGrounded)
{
if(Input.GetButtonDown("Jump"))
{
moveDir.y = jumpSpeed;
}
}
// calculate movement on the y-axis with gravity applied
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
PlayerCamera.js
var target : Transform;
function Start ()
{
}
function Update ()
{
transform.LookAt(target);
transform.position.x = target.position.x;
transform.position.y = target.position.y;
}
Answer by JanWosnitza · Oct 16, 2012 at 04:28 PM
You can use Vector3.SmoothDamp. The provided example is exactly showing how you could achieve your camera.
Wuld doing a Vector3.Lerp() have a similar effect to the above function?
Ive tried it out now and it works like a charm :D, perhaps a little bit loose at times but ill play around with it