26356

Unity bouncing when colliding with object

Question:

I have made a script with movement, that finally works as i want it to, except one thing... I want it to be a first person game, but the way the movement works now, is on the global axis, which means that W is always torwards one specific direction, no matter what direction my camera is turning... How do i fix this? i want the movement to stay how it is, but with the W key to always be forward depending on the way the camera or player is looking.

Please let me know how my script would look edited, or atleast what part i have to change.

I would also like to add that i would love to be able to do a wall jump, but i am not sure how to add that behavior.

Here is my movement script:

public class MovementScript : MonoBehaviour { public float speed; public float jumpforce; public float gravity = 25; private Vector3 moveVector; private Vector3 lastMove; private float verticalVelocity; private CharacterController controller; // Use this for initialization void Start () { controller = GetComponent<CharacterController> (); //Låser og gemmer musen Cursor.lockState = CursorLockMode.Locked; } // Update is called once per frame void Update () { //Låser musen op if (Input.GetKeyDown ("escape")) Cursor.lockState = CursorLockMode.None; moveVector = Vector3.zero; moveVector.x = Input.GetAxis("Horizontal"); moveVector.z = Input.GetAxis("Vertical"); if (controller.isGrounded) { verticalVelocity = -1; if (Input.GetButton("Jump")) { verticalVelocity = jumpforce; } } else { verticalVelocity -= gravity * Time.deltaTime; moveVector = lastMove; } moveVector.y = 0; moveVector.Normalize (); moveVector *= speed; moveVector.y = verticalVelocity; controller.Move (moveVector * Time.deltaTime); lastMove = moveVector; } }

Answer1:

You need to go from local to world space:

for example when you want to move on the x-axis regarding the player's orientation, the local vector is (1, 0, 0), which you get from your input axis, but the CharacterController need a world based direction (see <a href="https://docs.unity3d.com/ScriptReference/CharacterController.Move.html" rel="nofollow">the doc of the Move function</a>)

<blockquote>

A more complex move function taking absolute movement deltas.

</blockquote>

To get this, use <a href="https://docs.unity3d.com/ScriptReference/Transform.TransformDirection.html" rel="nofollow">Transform.TransformDirection</a> like this

worldMove = transform.TransformDirection(moveVector); controller.Move (worldMove * Time.deltaTime);

<strong>EDIT</strong> regarding your issue with the controller moving a bit after releasing the input:

That's because GetAxis gives you a smoothed value. Replace GetAxis by GetAxisRaw and it should work

Answer2:

Modify the final moving code to

controller.Move(moveVector.z * transform.forward * Time.deltaTime); controller.Move(moveVector.x * transform.right * Time.deltaTime); controller.Move(moveVector.y * transform.up * Time.deltaTime);

Alternatively, suppose your character is <strong>only rotated about the Y axis</strong>, you can look into rotating your moveVector by your character's Y rotation so moveVector's forward points parallel to your chracter's forward.

I found a good explaination of rotating a vector here: <a href="https://answers.unity.com/questions/46770/rotate-a-vector3-direction.html" rel="nofollow">https://answers.unity.com/questions/46770/rotate-a-vector3-direction.html</a>

Rotating a vector:

moveVector = Quaternion.Euler(0, transform.eulerAngles.y, 0) * moveVector;

Recommend

  • Regarding starting the threads on a condition
  • How to access profile picture with Facebook API in Swift 3?
  • How to use jQuery's $.post() method with async/await and typescript
  • Question about instantiating object
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • ASP.NET MVC Application won't update some controllers
  • Angularjs pass function from Controller to Directive (or call controller function from directive) -
  • Checking if an array in C is symmetric
  • c# open webrowser in many tab
  • Bypass multiple inheritance in Java
  • CakePHP ACL tutorial initDB function warnings
  • how to find common suffix in java by using method
  • Display java JPanel in a JFrame
  • onBackPressed() not being executed
  • iOS: Detect app start via notification press
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Jetty Server not starting: Unable to establish loopback connection
  • how to adjust image in a panel in Java swing?
  • How to access EntityManager inside Entity class in EJB3
  • Debugging ASP.NET on a built-in web server suddenly stops
  • how to do an event when i swipe from fragment to the other
  • JFileChooser in front of fullscreen Swing application
  • HTML download movie download link
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • Eraser for UIBezierPath
  • bootstrap to use multiple ng-app
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • python regex in pyparsing
  • Suggestions to manage Login/Logout transitions
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Android Google Maps API OnLocationChanged only called once
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Binding checkboxes to object values in AngularJs
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How to load view controller without button in storyboard?