- Home /
addforce + persp camera movement weirdness
I'm using mouse position to control the movement of an object on screen, basically wherever you click pushing the object in the opposite direction and how long you hold the mouse down for the speed.
Mostly it's fine, but every few clicks or seconds it comes back with the wrong direction pulling the object toward the mouse instead of pushing it away.
Using an ortho camera instead of persp seems to work fine, so it's something to do with that but I can't figure out how to fix it. And turning off the oscillation doesn't help.
Here's my code:
function Start() {
particleEmitter.emit = false;
yield WaitForSeconds(5);
particleEmitter.emit = true;
}
function Update () {
if(Input.GetButtonDown("Fire1")) {
clickStart = Time.time;
mousePos = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
currentPos = Vector3(transform.position.x, transform.position.y, transform.position.z);
moveDir = mousePos - currentPos; // persp camera, change for orth
moveDir.z = 0; //don't move the object on the z axis
moveDir.Normalize();
}
if(Input.GetButtonUp("Fire1")) {
clickEnd = Time.time;
//speedCalc = Vector3.Distance(mousePos, currentPos); //distance pointer from seed to set the speed
speedCalc = clickEnd - clickStart; //time mouse button held to set the speed
vel = Mathf.Clamp((speedCalc / moveSpd), minVel, maxVel);
rigidbody.AddForce(moveDir * vel, ForceMode.Impulse);
}
oscillatePos = Vector3(transform.position.x, transform.position.y, transform.position.z);
transform.position.y = oscillatePos.y + Mathf.Sin(Time.time * 1.5) * oscillateDist;
transform.position.x = oscillatePos.x + Mathf.Sin(Time.time * 1) * oscillateDist;
}
Answer by Anxo · May 29, 2011 at 08:34 PM
Not sure if it would help but you should always have Rigidbody activity like AddForce in a FixedUpdate instead of an update, the forece might be applied multiple times and does wired things in Update.
on a none related note, is there a reason why you use CurrentPos = Vector3(transform.position.x, transform.position.y, transform.position.z); instead of CurrentPos = transform.position? I am still learning a lot and I wonder if there is a benefit to it because I thought with checking for x y z independently would force the code to look for the game object's transform 3 times.
The mouse click has to be in update to always be detected, and because the addforce only occurs on button up, not every frame, it seems ok.
The currentPos is probably just leftover from when I was assigning the x and y separately (without z). I should change it.
Thanks for the suggestions.
did you find a solution? maybe try a Debug.Log just in front of the AddForce to see when the problem happens if it is called more than one time on the problem click.
Your answer
Follow this Question
Related Questions
Rigidybody2D addforce at constant speed 1 Answer
Not understanding how rigidbody2D.AddForce works 0 Answers
A flaw in my 2d chase or flee script 1 Answer
Player launches into air when hitting steep slopes 0 Answers
Declaring myRigidbody 1 Answer