Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 wobser · Jul 26, 2015 at 07:15 PM · beginner

Trying to get my pong AI to work

So I am a total beginner in coding. I am trying to get my Pong AI to work and I am using the Vector3.MoveTowards. I have searched for people with similar problems but often I don't understand the answers given. Or the answer is given in Java and I don't know how to convert it to c#.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
 
     Transform Ball;
     public float speed = 10;
     float movey;
 
 
     void Start()
     {
         Ball = GameObject.FindGameObjectWithTag ("Ball").transform;
     }
     // Update is called once per frame
     void Update () 
     {
         Vector3 giPos = Ball.transform.position;
         giPos.y = movey;
         Ball.transform.position = giPos;
         float step = speed * Time.deltaTime;
         transform.position = Vector3.MoveTowards (transform.position, movey, step);
     }
 }
 

Anyway, this is my code so far. It returns an compiler error in unity saying: CS1502: The best overloaded method match for UnityEngine.Vector3.MoveTowards(UnityEngine.Vector3, UnityEngine.Vector3, float)' has some invalid arguments And also: CS1503: Argument #2' cannot convert float' expression to type UnityEngine.Vector3'

I understand that the "movey" thing is what is causing the error but I am at a complete loss on how to fix it. I feel stupid for asking but I'm banging my head against the wall. Feels like I have tried everything.

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

1 Reply

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

Answer by shay4545 · Jul 26, 2015 at 07:23 PM

the problem is that movey is a float and Vector3.MoveTowards requires a Vector3 instead of a float where movey is. So an easy fix would be to add:

 Vector3 newPos = new Vector3(someXValue, movey, someZValue);

and then replace movey in the Vector3.Movetowards with newPos. Also, you might want to change:

 giPos.y = movey

to

 movey = giPos.y

Comment
Add comment · Show 3 · 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 wobser · Jul 26, 2015 at 10:43 PM 0
Share

Thank you so much for the help! This solved it for me. Just out of curiosity, why is it that in

Vector3 newPos = new Vector3(someXValue, movey, someZValue);

I get an compiler error when i put for example 8.5 as the x value. Works great as long as I use a whole number like 7,8 or 9. Not a problem, just curious. :)

avatar image shay4545 · Jul 26, 2015 at 10:46 PM 1
Share

Since 8.5 is not a whole number, you mst put an f after it to indicate it is a float. Whole numbers are ints, so they dont need the f after. Change it to 8.5f

avatar image wobser · Jul 26, 2015 at 10:53 PM 0
Share

Aha I see, thank you so much for the explanation. You have helped me a lot, keep up the good work! thumbsup

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how do I save a script when access is denied? 0 Answers

LoadLevel when colliding an object and clicking on it ? 1 Answer

Months after I left my Bootcamp demo alone, I can't see anything anymore.. 1 Answer

Static Variables Issue 0 Answers

Unity C# question 2 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