- Home /
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
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?
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
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;
}
}
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.
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
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);
I believe that you intended to say that Transform.position is Vector3 -- not Vector2.