Hello,
So, I wanted to created a paddle in my game that could move up and down, or have a range in its Y setting. Also, I do not like the snappy movement of just setting my paddle location to mouse location. So, with those two goals, I set out to find the functions/methods I needed to do that. The good news is, I found them and it works. However, in creating my solution, the paddle does not go straight to the location of the mouse, it only moves up,down,left,right, and at 45 degree angles. So if the paddle is told to move to location 10, 5 for example, and it is currently at 0,0 - then it will move to 5,5 and then follow Y5 axis to X10. I thought that the reason it did this was because I was running two separate Mathf.MoveTowards functions for the x and the y. So, in looking at the Unity Docs, I was confident that Mathf.MoveTowards took Vector3’s for its arguments, (current loc(Vector3), target loc(Vector3), time). However, when I take the specific axis out of the MoveTowards command, it throws an error saying cannot convert vector 3 to float. I feel confident I am doing something wrong, as constantly pulling the individual axis out to make move commands seems silly. Even from just a desire to refactor, telling my paddle what point to move to shouldn’t take 2 commands in 2D, or 3 commands in 3D. Anyway, here is my code. BTW, I created an empty object called mouse target. It is not needed, but I tried it as a solution to get a Vector from another object to see if that would make the method happy, instead of the Vector of the mouse pointer.
using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour {
public MouseTarget mouseTarget;
private Vector3 paddleTarget;
public static int paddle_speed_multiplier = 6;
public void Update () {
paddleTarget = mouseTarget.transform.position;
float speed = paddle_speed_multiplier * Time.deltaTime;
Vector3 paddlePos = new Vector3(this.transform.position.x,this.transform.position.y,0f);
paddlePos.x = Mathf.MoveTowards(this.transform.position.x,paddleTarget.x,speed);
paddlePos.y = Mathf.MoveTowards(this.transform.position.y,paddleTarget.y,speed);
this.transform.position = paddlePos;
}
}
So this works great. But that paddlePos.x and paddlePos.y, it needs to be refactored to paddlePos right?
So, I tried this…
using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour {
public MouseTarget mouseTarget;
private Vector3 paddleTarget;
public static int paddle_speed_multiplier = 6;
public void Update () {
paddleTarget = mouseTarget.transform.position;
float speed = paddle_speed_multiplier * Time.deltaTime;
Vector3 paddlePos = new Vector3(this.transform.position.x,this.transform.position.y,0f);
paddlePos = Mathf.MoveTowards(this.transform.position,paddleTarget,speed);
this.transform.position = paddlePos;
}
}
When I try that I get this…
Yet Unitys own docs say "public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);
What am I doing wrong? I really tried to research and figure this out. I created a empty object called mouse target which is essentially my mouse position put into its x and y clamps and put into a transform for a vector. I have played with this a hundred different ways and I can’t figure it out. Thanks for the help. Batsign - @Rob