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 animeman0 · Jul 13, 2015 at 04:31 PM · errortransformvector2

creating a Vector2 using transform.position not working - c#

What's wrong with this code:

 public Vector2 newPos = new Vector2 (transform.position - 45,0);
 public Vector2 negativeNewPos = new Vector2 (transform.position + 45,0);

Assets/scripts/EnemyScript.cs(12,72): error CS1502: The best overloaded method match for `UnityEngine.Vector2.Vector2(float, float)' has some invalid arguments

is there something wrong with transform.position

Comment
Add comment · Show 6
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 Aridez · Jul 13, 2015 at 05:42 PM 0
Share

What exactly do you want to do with that 45,0? Is that another vector with the components {x,y} = {45,0}? Or you want to add the scalar 45.0 to the vector?

avatar image animeman0 · Jul 13, 2015 at 05:51 PM 0
Share

Sorry about that I fixed it myself but I have a new problem. The gameobject that the script is attached to glitches then falls of the edge before detecting the layer "player

avatar image animeman0 · Jul 13, 2015 at 05:52 PM 0
Share
 using UnityEngine;

using System.Collections;

public class EnemyScript : $$anonymous$$onoBehaviour {

 bool seenPlayer;
 RaycastHit2D seePlayer;

 public Rigidbody2D rb;


 public Vector2 newPos = new Vector2 (-45,0);
 public Vector2 negativeNewPos = new Vector2 (45,0);

 float maxSpeed = 10f;
 Animator anim;

 GameObject player;

 public bool facingRight = false;



 void Start()
 {
     anim = GetComponent<Animator> ();
     rb = GetComponent<Rigidbody2D> ();
     player = GameObject.FindWithTag("Player");
 }

 void Update()
 {

 }

 void FixedUpdate()
 {
     //Just a debug visual representation of the Linecast, can only see this in scene view! Doesn't actually do anything!
     Debug.DrawRay(transform.position, newPos , Color.magenta);
     Debug.DrawRay(transform.position, negativeNewPos , Color.magenta);

     
     if(Physics2D.Raycast(transform.position,newPos, 1 << Layer$$anonymous$$ask.NameToLayer("Player")))
     {
         //we store the collider object the Linecast hit so that we can do something with that specific object, ie. the guard
         //each time the linecast touches a new object with layer "Player", it updates 'interacted' with that specific object instance
         seePlayer = Physics2D.Raycast(transform.position,newPos, 1 << Layer$$anonymous$$ask.NameToLayer("Player"));
         seenPlayer = true; //since the linecase is touching the player and we are in range, we can now interact!
         if(seePlayer)
         {
             Debug.Log("worked");
             anim.SetFloat("Speed",$$anonymous$$athf.Abs(1));
             rigidbody2D.velocity = new Vector2(2, rigidbody2D.velocity.y);
         }
     }
     else if(Physics2D.Raycast(transform.position,negativeNewPos, 1 << Layer$$anonymous$$ask.NameToLayer("Player")))
     {
         //we store the collider object the Linecast hit so that we can do something with that specific object, ie. the guard
         //each time the linecast touches a new object with layer "Player", it updates 'interacted' with that specific object instance
         seePlayer = Physics2D.Raycast(transform.position,negativeNewPos, 1 << Layer$$anonymous$$ask.NameToLayer("Player"));
         seenPlayer = true; //since the linecase is touching the player and we are in range, we can now interact!
         if(seePlayer)
         {
             Debug.Log("worked");
             anim.SetFloat("Speed",$$anonymous$$athf.Abs(1));
             rigidbody2D.velocity = new Vector2(-2, rigidbody2D.velocity.y);
             Flip();
         }
     }
     else
     {
         seenPlayer = false; //if the linecast is not touching a guard, we cannot interact
     }
 }
 void Flip ()
 {
     facingRight = ! facingRight; // flips the character
     Vector3 theScale = transform.localScale; //flips the local scale
     theScale.x *= -1; //flip the x axis 
     transform.localScale = theScale; //apply all of this back to the local scale
 }
 void movingRight()
 {
     Vector3 moveRight = transform.localScale;
     moveRight.x *= -1;
     transform.localScale = moveRight;
 }

}

avatar image Aridez · Jul 13, 2015 at 06:12 PM 0
Share

A bit hard to debug a big code like this one, in case you cannot manage to fix it give a little context on what is going on, what is happening exactly with that glitch and let us know how it goes.

avatar image animeman0 · Jul 13, 2015 at 06:25 PM 0
Share

I think it has somthing to do with:

 if(Physics2D.Raycast(transform.position,newPos, 1 << Layer$$anonymous$$ask.NameToLayer("Player")))
      {
          seePlayer = Physics2D.Raycast(transform.position,newPos, 1 << Layer$$anonymous$$ask.NameToLayer("Player"));
          seenPlayer = true; //since the linecase is touching the player and we are in range, we can now interact!
          if(seePlayer)
          {
              Debug.Log("worked");
              anim.SetFloat("Speed",$$anonymous$$athf.Abs(1));
              rigidbody2D.velocity = new Vector2(2, rigidbody2D.velocity.y);

I'm not sure though

Show more comments

1 Reply

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

Answer by masterhero · Jul 13, 2015 at 06:05 PM

Transform.position is Vector2. Maybe this is what you were trying to obtain:

 public Vector2 newPos = new Vector2(transform.position.x - 45, 0);
 public Vector2 negativeNewPost = new Vector2(transform.position.x + 45, 0);


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 JoshSims · Apr 17, 2018 at 09:36 AM 1
Share

I believe that you intended to say that Transform.position is Vector3 -- not Vector2.

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

24 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

Related Questions

I am getting an error and cant figure out why, Help please? 1 Answer

Cannot cast form source type to destination type? 2 Answers

moving objects with transform position 2 Answers

How do I detect changes in my vector2 positions? 1 Answer

unexpected convert float to int error 1 Answer


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