- Home /
UnityEngine.Vector2 Float conversion problem
Hi, I'm not too well known with C# but I wanted to learn it and picked up on it via Unity with a youtube video. I'm trying to follow a video guide on how to make a 2D game with my own twists to it but I'm kind of stuck now as I want to add an animation to the player's movement but all I get is 2 error messages that say I can't add any addition or function to the UnityEngine.Vector2.right and that I can't convert the type of float to the UnityEngine.Vector2. Does anybody know what my mistake is?
This is my code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerWalk : MonoBehaviour {
Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update ()
{
Movement ();
{
float move = Input.GetAxis("Horizontal");
anim.SetFloat ("Speed", move);
}
}
void Movement()
{
if (Input.GetKey (KeyCode.D)) {
transform.Translate (Vector2.right = 3f * Time.deltaTime);
transform.eulerAngles = new Vector2 (0, 0);
}
if (Input.GetKey (KeyCode.A)) {
transform.Translate (-Vector2.right = 3f * Time.deltaTime);
transform.eulerAngles = new Vector2 (0, 0);
}
}
}
Answer by Cynikal · Jul 11, 2017 at 11:32 PM
These lines: transform.Translate (-Vector2.right = 3f * Time.deltaTime);
Well, you can't do that.
Transform.Translate requests a Vector3.
You're also trying to change the value of a predetermined value (Vector2.right = your new value). Since Vector2.Right = 1,0, you could do something along the lines of:
transform.Translate(new Vector3(1, 0, 0) 3f Time.deltaTime);
Then just change the 1 to -1 for your left.
Answer by Milo_del_mal · Jul 12, 2017 at 12:26 AM
transform.Translate (Vector2.right = 3f * Time.deltaTime);
Change your "=" to "*".
if it still gives you problem use Vector3.right, but it should not.
Your answer
Follow this Question
Related Questions
Random float parameter for idle animations 1 Answer
Simple question about iTween with float 0 Answers
Get distance between 2 gameObjects? 2 Answers
Change animation at runtime with Mecanim 0 Answers