Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Mar 03, 2017 at 02:39 PM by PresidentPorpoise for the following reason:

Other

avatar image
0
Question by PresidentPorpoise · Mar 05, 2017 at 03:28 PM · 2dtransformsprite2d gamespawn

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);
     }
 }
 
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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

how do I change a 2D Sprite Character Image/Graphic to another Sprite Character Image/Graphic with the same movements, animation,mechanim etc. 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