- Home /
I need to make the direction.x negative without the direction.y being negative any help?,Hi so i need to change the direction.x to negative without the direction.y being negative could anyone know how?
this is the script using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DashMove2 : MonoBehaviour
{
private Vector2 targetPos;
private Vector2 direction;
public PlayerController pc;
public float dashRange;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (pc.facingRight == true)
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 myPos = transform.position;
Vector2 direction = (mousePos - myPos).normalized;
transform.Translate(direction * dashRange);
}
else if (pc.facingRight == false)
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 myPos = transform.position;
Vector2 direction = (mousePos - myPos).normalized;
transform.Translate(direction * dashRange);
}
}
}
}
basically im trying to do something like a dodge but when i flip my sprite the dodge goes in the wrong direction so i need to make the direction.x negative but i dont know how to split Vector2 into direction.x and direction.y and then put in in the transform.Translate ,this is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DashMove2 : MonoBehaviour
{
private Vector2 targetPos;
private Vector2 direction;
public PlayerController pc;
public float dashRange;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (pc.facingRight == true)
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 myPos = transform.position;
Vector2 direction = (mousePos - myPos).normalized;
transform.Translate(direction * dashRange);
}
else if (pc.facingRight == false)
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 myPos = transform.position;
Vector2 direction = (mousePos - myPos).normalized;
transform.Translate(direction * dashRange);
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to create a field of view for enemy ai that detects a player Unity 2d? 3 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Scale fishing rod in one direction 2 Answers