Error with float/vector

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);

Mathf.MoveTowards

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

Hi,

Which version of Unity are you using?

I have just searched the documentation (2017/2) and it states that this method will take floats not vectors, which would align with the error message you are receiving.

Ok, found the issue. Vector3 has a method called MoveTowards as does Mathf. I believe you’ll want the Vector3 version to avoid your error message.

I can’t believe that was the problem. When Ben did it in the tutorial, mathf worked fine because he was only affecting one axis with movement. I didnt even think about that part. I needed a separate set of eyes to see something right in my face. Thanks again @Rob Solved!

1 Like

Happy to help!

As I was using my phone it was more challenging to spot the differences in documentation initially, I thought perhaps it was an overloaded method, so it was useful for myself also :slight_smile:

Glad you have it sorted :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms