0
\$\begingroup\$

Backstory

I'm currently working on a 2D sidescroller/platform game as a hobby, and I just started recently. Followed a few tutorials and such I managed to wrote my own code for basic command like walk, jump, health and so on. However my basic code wasnt perforning as I would expect, I needed a more "Tight" feeling with the movements so I followed a suggestion to use a physics based controller (Prime31's PC2D, Acrocatic or even Corgi Engine...)

Well, now the movements are what I feel is good, So I kind of dived in the scripts to add/remove/customize some code.

Question

I'm stuck now on a simple issue, I added these lines

public Transform weaponMuzzle;
public GameObject projectile;
float fireRate = 0.5f;
float nextFire = 0;

then in my Update()

if (Input.GetButtonDown("Fire2"))
{
  animator.Play.Weapon();
  fireProjectile ();
}

and here is the problem I have

void fireProjectile ()
{
  if (Time.time > nextFire) 
  {
    nextFire = Time.time + fireRate;

    if (_motor.facingLeft)
    {
      Instantiate (projectile, weaponMuzzle.position, Quaternion.Euler (new Vector3 (0,0,180f)));
    }
    else if (!_motor.facingLeft)
    {
      Instantiate (projectile, weaponMuzzle.position, Quaternion.Euler (new Vector3 (0, 0, 0)));
    }
  }
}

The prefabs are also physics based and they are fired with no problems, however they don't want to follow the current rotation of the player, they always go in the right X axis.

First of all, the facingLeft on my code is what I understand is the reference to the character controller motor, I tried with _motor.normalizedXMovement > 0 and _motor.normalizedXMovement < 0 (normalizedXMovement -1 is full left and 1 is full right) this just make it that you can only fire while moving, not stopped and the problem still persists.

I also tried with Quaternion.Identity instead of Euler and it's the same.

For a full source code : https://github.com/prime31/CharacterController2D

I'm kinda lost and it would means a lot if you can help me out on this issue.

Ps: I already searched here and other related forums but cannot find the answer, this is not a repost.

\$\endgroup\$
1
\$\begingroup\$

Wanted to make a comment but new to Gamedev so sorry about that

I haven't been able to find the snippet of code you provided on github. However you have several options to get the rotation working:

  • Depending on how you handle the rotation towards the cursor you could simply reference the transform of the muzzle / player object when instantiating your projectile. I'm going to assume that's what you are trying to do now.

  • Using Quaternion.SetLookRotation(RayCastHit.position - transform.position)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.