Rigidbody on player becomes shaky on collision
I'm having problems with my character's Rigidbody. Basically I have two scripts both are which attached to the player, the first script is my player movement script which also have a Addforce bounce variable with it and the second script is a disabling script which disable my player movement script for two seconds so my bounce variable can work. As mention above my I'm having problems with the Rigidbody attach to my player. When my player collides with an object that is set to trigger, my players movement gets really weird and shaky from the direction of where the object came from, until the player collides with another box collider then it goes back to normal!!! How would I stop this from occurring? Here are all three scripts. Thank you:
Disabling script:
using UnityEngine;
using System.Collections;
public class Disablingscript : MonoBehaviour {
public Playermovement playerMovementRef;
void Start ()
{
StartCoroutine("DisableScript");
}
IEnumerator DisableScript ()
{
playerMovementRef.enabled = false;
yield return new WaitForSeconds(2f);
playerMovementRef.enabled = true;
}
}
Player movement script:
using UnityEngine;
using System.Collections;
public class Playermovement : MonoBehaviour {
public float speed = 15f;
private Vector3 target;
void Start () {
target = transform.position;
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Bouncy object")
GetComponent<Rigidbody2D>().AddForce(transform.right * 500);
}
}
Answer by Statement · Nov 11, 2015 at 12:03 AM
/* somewhere */
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
/* elsewhere */
GetComponent<Rigidbody2D>().AddForce(transform.right * 500);
How would I stop this from occurring?
Don't mix modiying transform and rigidbody for motion. If your game uses physics to drive motion, move your character with forces instead. Physics engine will update rigidbodys position and then you will just overwrite the position calculated by the physics engine so your character is kinda like walking in two directions at once.
AddForce applies acceleration, meaning your object maintains a velocity (that may dissipate due to friction and drag, but still, it applies motion over several frames/seconds at least). Setting transform.position just totally ignores what physics is doing and both of them update the position in different directions.
All movement should use the rigidbody unless you know what you are doing.
/* Awake */
rb = GetComponent<Rigidbody2D>();
/* FixedUpdate */
// No idea if this is still valid, but it's a start.
rb.AddForce(Vector3.MoveTowards (rb.position, target, speed * Time.deltaTime));
/* elsewhere */
rb.AddForce(transform.right * 500);
Adjust the code to feel right as it's no longer applying velocity but rather acceleration.
Thank you for replying back, I've tried out what you suggested, however my players movement is now slow. For example I have to click multiple times for my player to get there. $$anonymous$$aybe I'm doing something wrong. Here's what I wrote: using UnityEngine;
using System.Collections;
public class Playermovement : $$anonymous$$onoBehaviour {
public float speed = 15f;
private Vector3 target;
public Rigidbody2D rb;
void Start () {
target = transform.position;
}
void Awake () {
rb = GetComponent<Rigidbody2D> ();
}
void Update () {
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
target.z = transform.position.z;
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, target, speed * Time.deltaTime);
}
}
void FixUpdate (){
rb.AddForce (Vector3.$$anonymous$$oveTowards (rb.position, target, speed * Time.deltaTime));
rb.AddForce(transform.right * 500);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Bouncy object")
GetComponent<Rigidbody2D>().AddForce(transform.right * 500);
}
}
Slow? Well that code wouldn't move the character with physics at all because "FixUpdate" is never called (It's called "FixedUpdate", not "FixUpdate"). But you still have your code in Update setting transform.position so until you remove that, you'll still get jerky movement.
Okay thanks, but what would I change transform.position too
Your answer
Follow this Question
Related Questions
How to play an animation on collision 1 Answer
Sphere bouncing back on edges of aligned objects 0 Answers
How can i put down a sprite on collision? 0 Answers
Change vertex normals on collision 0 Answers