Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by petediddy · Apr 30, 2015 at 05:55 AM · c#transformaddforce

Smooth out character movement?

Hi everyone,

I've done a bunch of searching for an answer, and can't seem to find anything that has helped. In a nutshell, my character's movement is jagged/jittery and I'm trying to smooth it out. However, there is a slight wrinkle compared to other answers I've researched: the character is currently being moved in 2 ways: 1) with a transform.Translate using a speed variable in the local z direction, and 2) when the user presses a button, I am calling rigidbody.AddForce in the y direction (resulting in a jump). Both of these forms of movement are occurring in the Update function in my character controller script. The character is a prefab, instantiated at run-time. The character moves smoothly in the z direction, but when it jumps or falls in the y direction the movement appears jagged and not smooth (sort of like a sawtooth, up/forward/up/forward/up/forward really fast... appears to be each frame from my guess).

At first, I thought the issue was my camera follow script, which uses a Vector3.Lerp to follow the character. However, when I disable the camera follow script so the camera doesn't move, the character's movement still appears jittery/jagged when moving up or down (same as when the camera follow script is enabled).

Any ideas how to fix? I was thinking of using a Lerp to move the character, but unsure how to determine what my target position would be. Maybe there's a better way. I've also tried using interpolate/extrapolate on the character's rigidbody, but this did not change anything.

-pete

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image petediddy · May 03, 2015 at 10:56 AM 0
Share

Here's my code for movement:

 //Player move forward
 transform.Translate (0.0f, 0.0f, zSpeed * Time.deltaTime);
 
 //Player jump - rb is the rigidbody of player
 rb.AddForce(0.0f, jumpYVelocity, 0.0f, Force$$anonymous$$ode.VelocityChange);

These commands are both in Update - would it really make a difference to move to FixedUpdate? I have the AddForce command in Update since it happens after input is received - was thinking that input needs to be received in the Update function, so the AddForce happens when input received in Update.

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Warren933 · Aug 07, 2015 at 12:05 PM

Why dont you add the camera as a child to the player, then freeze the undesired rotation to allow it to face only the direction you want it to?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by anubhavsd · Apr 30, 2015 at 06:40 PM

Try FixedUpdate instead of update and sync movement accordingly.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by JayFitz91 · Apr 30, 2015 at 06:48 PM

Try using this, It's what I use for forward character movement, you should also post your code in the future so we can see what you're working with

 transform.Translate(Vector3.forward * speed * Time.fixedDeltaTime, Space.Self);

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image petediddy · Apr 30, 2015 at 07:36 PM 0
Share

Ok, so I have moved the transform.Translate to FixedUpdate, as well as the rb.AddForce. The code for player movement is below. However, now ins$$anonymous$$d of the jagged sawtooth movement, it now appears that the player is lurching forward as it moves (camera movement script is disabled). Any more ideas I can try?

 void FixedUpdate()
     {
         transform.Translate (0.0f, 0.0f, zSpeed / 50, Space.Self);
 
         if (jumpInputReceived == true) {
             rb.AddForce (0.0f, jumpYVelocity, 0.0f, Force$$anonymous$$ode.VelocityChange);
             jumpInputReceived = false;
         }
     }
avatar image JayFitz91 · Apr 30, 2015 at 09:28 PM 1
Share

This code will move your character forward in the direction he is facing, just make your camera a child of your character.

I have just tested this myself and everything works, movement in all directions and jumping, and everything runs smoothly.

There is a reference to this script in the unity documentation here

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterController))]
 public class movement : $$anonymous$$onoBehaviour 
 {
 
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     public float rotateSpeed = 6;
     private Vector3 moveDirection = Vector3.zero;
 
     CharacterController controller;
 
     void Start()
     {
         controller = GetComponent<CharacterController>();
     }
     void Update()
     {
 
         if (controller.isGrounded)
         {
             moveDirection = new Vector3(0,0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
 
             transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
 
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
 
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
     }
 }


avatar image
0

Answer by petediddy · May 01, 2015 at 04:07 AM

Thanks JayFitz91 ! That code has made the character movement really smooth now. Unfortunately, when I turn on the camera follow script, it still looks jittery. I tried making the camera a child of the player, but I don't want the camera to rotate like the player does, so I have used the below script on the camera to help it follow the player, but there is still some jitter. Any help? My camera follow script code is below:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
     
     public float smoothing = 1.0f;
     public Transform target;
 
     private Vector3 offset;
 
     void Start()
     {
         //Assign offset for camera
         offset = transform.position - target.transform.position;
     }
 
     void LateUpdate()
     {
         //If target is still valid, it means the player has not been destroyed yet, so move camera
         if (target != null) 
         {
             Vector3 targetCamPos = target.transform.position + offset;
             transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing);
         }
     }
 
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image B19g_A5s_B411s · Aug 11, 2015 at 03:09 PM 0
Share

Have you tried moving the camera script into FixedUpdate() Camera movement, sway, etc. is usually jerky for me until i put it in fixed update.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Move 3D Cubes with mouse 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

how to find a point(vector2) between two points(Vector2) of a line (line renderer) 2 Answers

Saving coordinates and transferring them to the second object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges