Constant Velocity?
I have tried to control my object with my mouse using this code:
using UnityEngine;
using System.Collections;
public class AstronautScript : MonoBehaviour {
public float SPEED; //In units per frame
Vector3 destination;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire2")) {
Vector3 pos = Input.mousePosition;
pos.z = 0;
destination = Camera.main.ScreenToWorldPoint(pos);
}
}
void FixedUpdate () {
Vector3 movement = destination - transform.position;
GetComponent<Rigidbody2D>().velocity = movement.normalized*SPEED;
}
}
As you can see I've normalized the movement vector, but when I play my game, the speed of the object is higher the farther away from the destination that it is. Anyone have an answer?
Answer by MelvMay · May 16, 2016 at 06:35 PM
Maybe stop using a Vector3 in 2D physics and use the correct Vector2. You'll not get the distance in 2D space if there's a difference in Z using your code.
Your answer
Follow this Question
Related Questions
Random freezes during the game 0 Answers
destroying a physics object after it starts moving? 1 Answer
Bounce not working when Velocity is below 1f , what could it be? 0 Answers
Android: Hiccups in simple scene - related to time 0 Answers
Collision: I don't know how to phrase what I am asking for 0 Answers