Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Serge144 · Aug 29, 2020 at 06:25 PM · cameraplayer movementcamera movementrigidbody.addforcerigid body

Camera relative rigidbody third person player movement

I have been trying to do this for the longest of times and still haven't found a solution. All I need is to do the following: when pushing upwards on the joystick, the player will move forward in the direction that the camera is facing (Camera follows the player but the rotation is fixed, only the user decides wether it should rotate it). The script should use AddForce since it's a non-kinematic Rigidbody and not MovePosition I have found multiple scripts and tried these scripts, but none takes into account what im trying to do:

  1. Unity Standard Assets ( ThirdPersonCharacter.cs ) - Does exactly what I want respective to the camera relative movement, but the movement is based on Animation and not on AddForce.

  2. RigidbodyFPSWalker (Well known script - look it up) - Doesn't take into account the camera.

  3. Some_really_simple_script_that_just_uses_MovePosition.cs" - Doesn't take into account the camera, and shouldn't use MovePosition, this method should only be used for Kinematic RigidBodies.

Please any help would be apreciated Thank you in advance.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Serge144 · Aug 29, 2020 at 08:17 PM

I built this script from parts of differents scripts and it's working! I only have some questions regarding this script.

 public class PlayerControllerRigid : MonoBehaviour
 {
     public float speed = 10.0f;
     public float maxVelocityChange = 10.0f;
     private new Rigidbody rigidbody;
     public Joystick joystick;
 
     void Awake()
     {
         rigidbody = GetComponent<Rigidbody>();
     }
 
     private void Update()
     {
         float horizontal = joystick.Horizontal;
         float vertical = joystick.Vertical;
 
         Vector3 input = new Vector3(horizontal, 0f, vertical);
         Vector3 inputV2 = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0) * input;
         if (inputV2 != Vector3.zero)
             transform.rotation = Quaternion.LookRotation(inputV2, Vector3.up);
     }
 
     void FixedUpdate()
     {
             float horizontal = joystick.Horizontal;
             float vertical = joystick.Vertical;
 
             // Calculate how fast we should be moving
   Vector3 camForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
             Vector3 targetVelocity = new Vector3(horizontal, 0, vertical);
             targetVelocity = transform.TransformDirection(targetVelocity);
             targetVelocity = vertical * camForward + horizontal * Camera.main.transform.right;
             targetVelocity *= speed;
 
             // Apply a force that attempts to reach our target velocity
             Vector3 velocity = rigidbody.velocity;
             Vector3 velocityChange = (targetVelocity - velocity);
             velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
             velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
             velocityChange.y = 0;
 
             rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
     }
 }


  1. Can I somehow improve the rotation of the player on the Update method? I dont want any rotation parameters, I just want to know if maybe its better to use AddTorque since its a rigidbody.

  2. The movement seems slightly jiggly (not as smooth as it should be), any idea why is that?.

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 Serge144 · Aug 30, 2020 at 04:07 PM 0
Share

Regarding 2), i researched and by changing the fixed timestep from 0.02 to 0.016 it fixed the jitter (Edit > Project Settings > Time > Fixed TimeStep). However I wanted to know if there's anything else in my code that is causing this jitter.

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

205 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using Camera Y Rotation to Determine Player Movement Direction 0 Answers

Rigidbody Controller move forward in the direction of the camera 0 Answers

How do I make my cameras parent follow my player? 0 Answers

Solve Issue of Camera looking through walls when in them 0 Answers

Camera Follow a Target, but not Centered on it 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