- Home /
Huh click and go script working 50%
um hey guys im the guy here. Eh so I recently tried to create a script where you click on a random place and then the script will move towards it as long as you are holding down the button. So what I did first is that I copied brackey's script for facing the mouse pointer all time onto my player character. Then I added sin and cos stuff to make the player moving in the direction I was clicking on.
so eh this is what I typed:
public class clickAndGoMovement1 : MonoBehaviour {
public float moveDistance = 5; public float moveX; public float moveY;
void Update () { Vector3 mousePlayerDifference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; mousePlayerDifference.Normalize(); float rotationZ = Mathf.Atan2(mousePlayerDifference.y, mousePlayerDifference.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0f, 0f, rotationZ); //it's now facing the mouse, now move it!
Vector3 myPosition = transform.position; if (Input.GetKey("space")) { moveX = (Mathf.Cos(rotationZ)) moveDistance Time.deltaTime; moveY = (Mathf.Sin(rotationZ)) moveDistance Time.deltaTime; myPosition.x += moveX; myPosition.y += moveY; transform.position = myPosition; }
} }
when I point my mouse pointer to a location and holds space. The player will turn and move towards my pointer. Tho that doesn't mean it is successful. Somehow, if I hold down space while my mouse pointer is moving, the player character will freak out for no reason, and shakes violently in place until I stop moving my cursor, then the freak stops and it starts moving to my cursor. I am not making this thing up I swear. And Im asking you guys out of desperation because I can't get any reply over at "unity answers". I think I messed up something with the sin and cos stuff, cause I think it convert the output to radian instead of normal numbers... IDK how I messed it up tho. Please help
Your answer
Follow this Question
Related Questions
How can I make player move with cursor?,How can I make player move with mouse? 1 Answer
How to control an object with a mouse? 1 Answer
I can'^t move object in seceted axis 0 Answers
Get general direction of mouse/tap location relative to player on mouse click? 0 Answers
Movement along X and Z axis... 2 Answers