Hi there,
I have an object that travels to my mouse position however it’s not behaving how I want it to. I want to make my object travel at a constant speed no matter what the magnitude of my vector is. At the moment, the closer the mouse pointer is the to object, the slower it travels, the further the faster. I tried normalizing but it’s still behaving the same way. Here is the code ~ IMPORTANT BITS LINE 50>:
Many thanks for any replies
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
float proxyAngle;
float speed = 20;
Vector3 vectorToTarget;
Vector3 mouseLocal;[/COLOR]
Rigidbody2D rb;
public float acceleration = 100;
public float moveSpeed = 5;
void Start () {
rb = gameObject.GetComponent<Rigidbody2D>();
}
void Update () {
if(Input.GetMouseButton(0))
{
mouseLocal = Camera.main.ScreenToWorldPoint(Input.mousePosition);
vectorToTarget = mouseLocal - transform.position;[/COLOR]
float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
proxyAngle = angle;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
}
}
void OnCollisionEnter2D(Collision2D coll) {
if(coll.gameObject.tag == "Floor")
{
Debug.Log ("FLOOR");
Application.LoadLevel(0);
}
if(coll.gameObject.tag == "Goal")
{
Debug.Log ("GOAL YE");
Application.LoadLevel(0);
}
}
void OnTriggerStay2D(Collider2D coll){[/COLOR]
if(coll.tag == "Ramp" && Input.GetMouseButton(0)) {
vectorToTarget = Vector3.Normalize (vectorToTarget);
Debug.Log (vectorToTarget);
rb.velocity = vectorToTarget;
//Debug.Log (vectorToTarget * moveSpeed);
//Rigidbody2D rb = gameObject.GetComponent<Rigidbody2D>();
//Vector3 currentVelocity = rb.velocity;
//rb.velocity = currentVelocity - mouseLocal;
}
}
}