- Home /
Smoother camera movement?
I have written a custom camera follow script which deals with specific requirements within my game. The camera smoothly tracks the player position, however surrounding geometry appears to "jitter" and the further away geometry is, the worse the problem appears.
I understand why this happens, moving the camera a little close up makes a larger jump further away.
What can I do to minimize this undesired effect?
Answer by DevonJavaScript · Feb 09, 2012 at 05:56 AM
In my experience with this the main problems come from either Lerping or the Function the lines are contained in:
Steps to Make Movement smooth:
1) Lerp positions:
var nextPosition: Vector3; //or public Vector3 nextPosition; if in C#
var moveSpeed: float; //or public float moveSpeed; if in C#
transform.position = Vector3.Lerp(transform.position, nextPosition, Time.deltaTime * moveSpeed);
2) Use an Update function instead of LateUpdate or FixedUpdate:
function Update() //Or void Update() if in C#
3) Use Time.deltaTime as its smoother than Time.time:
Time.deltaTime;
I really hope I could help be sure to ask if any further Questions.
Okay, having made that change the camera is now moving beautifully....but, now the player is jittering. The player has a rigidbody and is moving via the rigidbody velocity. cheers
To clarify, I have added Vector3.Lerp
. I was already using Update
...I didn't even know of FixedUpdate
so I will read into that. Any ideas for smoother player movement would be brilliant thanks again
Try to stay away from Rigidbody controllers, I wrote a character movement script for you were you only need to attatch the Character Controller script: private var moveDir: Vector3; private var CurrentSpeed: float = 0.0; var RunSpeed: float = 8.0; var WalkSpeed: float = 6.0; var JumpSpeed: float = 12.0; var gravity: float = 20.0; function Update () {
//Toggle Run
if(Input.GetButton("Run")){
CurrentSpeed = $$anonymous$$athf.Lerp(CurrentSpeed,RunSpeed, Time.deltaTime * 3.5);
}else{
CurrentSpeed = WalkSpeed;
}
var controller : CharacterController = GetComponent(CharacterController);
grounded = controller.isGrounded;
//the meat of the script
if(controller.isGrounded){
moveDir = Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
moveDir = transform.TransformDirection(moveDir); //How to move
moveDir *= CurrentSpeed;
if(Input.GetButton("Jump")){
moveDir.y = JumpSpeed;
}
}
controller.$$anonymous$$ove(moveDir * Time.deltaTime);
moveDir.y -= gravity * Time.deltaTime;
}
Wow, my player is moving very nicely now! Except, now my player has no RigidBody, how can it collide with things? I am making a side-scroller shooter which has various obstacles, walls, etc. I haven't applied a CharacterController
because the properties did not seem relevant for my character. How come the RigidBody
causes such jittering? I really appreciate your help
Yeah! Just with the Character Controller the Controller Acts as a collider, so if you goto unity documentation you can look at character controller and look at its functions and see it also returns as a collider. You can call the collider functions with the function just as most functions for collision do!
Answer by Vishwa001 · May 26, 2017 at 08:15 PM
Best way to smooth camera movement in untiy2D..
transform.position=Vector3.SmoothDamp(new Vector3(transform.position.x,transform.position.y,-10f), new Vector3(player.transform.position.x,player.transform.position.y,-10f), ref velocity, smoothTime);
Your answer
Follow this Question
Related Questions
How to create an old school camera switch when the player leaves/enters camera view 1 Answer
2.5D lighting looks wrong when rotating camera 0 Answers
2.5D Shooter Rescrict Movement to Camera View 2 Answers
How do I implement this Camera Tracking? 0 Answers
Need to Find Distance Between Mouse Cursor and Object in a 2.5D SideScroller 1 Answer