- Home /
Question by
Xysch · Oct 07, 2014 at 11:33 PM ·
movementplayermouserigidbody2d
Why does it seem that rigidbody seems of when moving the character?
What I have:
So I'm pretty new to C# but know a few other codes. Everything I am doing is in Unity2D. Now I have gotten my character to move using the mouse and that it has like a "lag" or "delay" and that the player moves faster the farther away your mouse is.The Problem:
Now the only problem with this, is whenever the player collides with objects, the player seems to studder and jerk around. My player also doesn't seem to put an effect on Rigidbody2D's objects anymore. Although I can slightly push floating blocks, they don't spin and rotate anymore like they did before. I beleive this might be from using transform and not using a force like with the other code I used, but what do I know.The C# Code:
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
public float speed = 3.0f;
public AudioClip thudSound;
public bool mouseClick = false;
private Vector3 targetPos;
void Start() {
targetPos = transform.position;
}
//Thud Sound
void OnCollisionEnter2D(Collision2D target){
var absVelX = Mathf.Abs (rigidbody2D.velocity.x);
var absVelY = Mathf.Abs (rigidbody2D.velocity.y);
if (absVelX <= .01f || absVelY <= .01f) {
if (thudSound)
AudioSource.PlayClipAtPoint (thudSound, transform.position);
}
}
void Update () {
//To turn on and off the follow mouse
if (Input.GetMouseButtonDown (0)) {
if(mouseClick == false){
mouseClick = true;
} else if (mouseClick == true){
mouseClick = false;
}
}
//Follow the mouse
if (mouseClick == false) {
float distance = transform.position.z - Camera.main.transform.position.z;
targetPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance);
targetPos = Camera.main.ScreenToWorldPoint (targetPos);
transform.position = Vector3.Lerp (transform.position, targetPos, speed * Time.deltaTime);
} else {
//Stop following but continue sliding
transform.position = Vector3.Lerp (transform.position, targetPos, speed * Time.deltaTime);
}
}
}
I would really appreciate it if you could help me understand this and thanks for your time!
Comment
Best Answer
Answer by sevensixtytwo · Oct 08, 2014 at 12:18 AM
You really shouldn't use Vector3.Lerp and its cousins for movement when you're talking rigidbodies. Collisions get all screwy.
rigidbody.MovePosition(targetPos * Time.deltaTime);
Your answer
Follow this Question
Related Questions
Can someone help me with my TPS Controller/Camera? 1 Answer
player move while mouse move. 0 Answers
Player not moving 1 Answer
Vertical mouse input inverted when player is turned around 0 Answers