- Home /
Other
Inverting Position on One Axis
Hello, I have been trying to achieve something for a long time and I am getting desperate. I have an object attached to my player sprite that marks the location that a projectile spawns at when the player clicks to shoot. The problem is, when I used FlipX to flip the player sprite, the object does not also go to the other side of the player, so the projectiles spawn in the wrong place.
Using C#, is there any way I could change transform.position.x to become negative and vice versa in order to make it "flip" with the player? I have tried doing this on my own but nothing I do apparently works with transform.position.
Also, is there a better way to achieve this? Thanks in advanced.
My code:
Player Movement Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float maxSpeed;
public float jumpSpeed;
public float checkRadius;
public bool onGround;
public bool facingRight;
private Rigidbody2D playerRB;
private Animator playerAnim;
private SpriteRenderer rend;
public Transform groundChecker;
public LayerMask groundLayer;
private void Start()
{
playerRB = gameObject.GetComponent<Rigidbody2D>();
rend = GetComponent<SpriteRenderer>();
playerAnim = GetComponent<Animator>();
facingRight = true;
onGround = true;
}
private void Update()
{
//if the player is on the ground and the spacebar has been pressed, the player jumps
if (onGround == true && Input.GetAxis("Jump") > 0)
{
onGround = false;
playerAnim.SetBool("Grounded", onGround);
playerRB.AddForce(new Vector2(0, jumpSpeed));
}
}
private void FixedUpdate()
{
//sets grounded to true if the overlapCircle overlaps something on the layer set as groundLayer
onGround = Physics2D.OverlapCircle(groundChecker.position, checkRadius, groundLayer);
float move = Input.GetAxis("Horizontal");
playerAnim.SetBool("Grounded", onGround);
playerRB.velocity = new Vector2(move * maxSpeed, playerRB.velocity.y);
playerAnim.SetFloat("speed", Mathf.Abs(move));
if (move < 0 && facingRight)
{
turn();
}
else if (move > 0 && !facingRight)
{
turn();
}
}
void turn()
{
facingRight = !facingRight;
rend.flipX = !rend.flipX;
}
}
Projectile Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WizardProjectile : MonoBehaviour {
private float speed;
public GameObject projectileSpawn;
public GameObject player;
public PlayerMovement playMov;
public BoxCollider2D col;
private Animator anim;
private SpriteRenderer rend;
private void Awake()
{
projectileSpawn = GameObject.Find("Wizard Projectile Spawn");
}
private void Start ()
{
//defining variables
speed = 15f;
player = GameObject.Find("Player");
col = GetComponent<BoxCollider2D>();
transform.position = projectileSpawn.transform.position;
rend = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
//makes projectile face the same direction as the player
rend.flipX = player.GetComponent<SpriteRenderer>().flipX;
}
private void Update()
{
// Will need to change all of this in order for the projectile to follow where the cursor clicked.
if (GetComponent<SpriteRenderer>().flipX == true)
{
transform.Translate(Vector2.left * Time.deltaTime * speed);
}
else if (GetComponent<SpriteRenderer>().flipX == false)
{
transform.Translate(Vector2.right * Time.deltaTime * speed);
}
Destroy(gameObject, 2);
}
private void OnTriggerEnter2D (Collider2D col)
{
anim.SetBool("Hit", true);
Destroy(gameObject);
}
}
Follow this Question
Related Questions
Problem with Scrip for sprites 1 Answer
Achieving something like LookAt but with the X axis. 3 Answers
Best resolution to create sprites at? 0 Answers
Sprite is not shown moving 1 Answer