Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rexasia · Jul 11, 2017 at 11:24 PM · animationfloatvector2

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);
     }
 }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rexasia · Jul 12, 2017 at 12:31 AM 0
Share

Thanks a lot!

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

168 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random float parameter for idle animations 1 Answer

Simple question about iTween with float 0 Answers

the run animation dont stops if i stop my charaster,hello, i cant stop the "run" animation, when speed == 0 1 Answer

Get distance between 2 gameObjects? 2 Answers

Change animation at runtime with Mecanim 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges