Hello Unity
I recently picked up Unity and already stumped upon an issue.
My project is a 2.5D with the background and platforms being 3D and everything else in 2D.
So far I’ve been able to flip both the player and the sprite of the projectile as intended. However, when it comes to shooting, I’m experiencing 2 problems:
-
I’m unable to addforce when facing left.
-
Angling - When I shoot facing right, I shoot up to the right (Imagine throwing a stone). So up and to the right. When facing left, it shoots straight downwards and due to problem 1, it’s just falling down, when instantiated.
Most of my codes have been handpicked and slightly modified to what I need so far.
I have been unable for 2 days to find a solution, so any tips and help would be appreciated, and I apologize in advance, if I am slow to understand any feedback, as I am relatively new to C#.
Below are my scripts
The following is my code for projectile:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectile : MonoBehaviour
{
public float speed = 150f;
Player player;
public bool facingRight = true;
private void Start()
{
player = GameObject.Find("Player").GetComponent<Player>();
if (!player.facingRight && transform.localScale.x > 0)
{
Flip();
}
}
private void OnTriggerEnter2D(Collider2D target)
{
if (target.gameObject.tag == "FirePoint") GetComponent<Rigidbody2D>().AddForce(transform.right * speed);
}
public void Flip()
{
facingRight = !facingRight;
transform.localScale = new Vector3(
transform.localScale.x * -1,
transform.localScale.y,
transform.localScale.z);
}
}
The following is where my projectile gets instantiated
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThrowingAxeSpawn : MonoBehaviour
{
public GameObject throwingAxe;
public Transform spawnPoint;
void FixedUpdate()
{
bool shoot = Input.GetKey(KeyCode.Mouse1);
{
if (shoot) Instantiate(throwingAxe, spawnPoint.position, spawnPoint.rotation);
}
}
}
The following is my player script if it has any relevance
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Controller2D))]
public class Player : MonoBehaviour
{
public bool facingRight = true;
public float maxJumpHeight = 4;
public float minJumpHeight = 1;
public float timeToJumpApex = .4f;
float accelerationTimeAirborne = .2f;
float accelerationTimeGrounded = .1f;
float moveSpeed = 6;
public Vector2 wallJumpClimb;
public Vector2 wallJumpOff;
public Vector2 wallLeap;
public float wallSlideSpeedMax = 3;
public float wallStickTime = .25f;
float timeToWallUnstick;
float gravity;
float maxJumpVelocity;
float minJumpVelocity;
Vector3 velocity;
float velocityXSmoothing;
Controller2D controller;
Vector2 directionalInput;
bool wallSliding;
int wallDirX;
void Start()
{
controller = GetComponent<Controller2D>();
gravity = -(2 * maxJumpHeight) / Mathf.Pow(timeToJumpApex, 2);
maxJumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
minJumpVelocity = Mathf.Sqrt(2 * Mathf.Abs(gravity) * minJumpHeight);
}
void Update()
{
CalculateVelocity();
HandleWallSliding();
controller.Move(velocity * Time.deltaTime, directionalInput);
if (controller.collisions.above || controller.collisions.below)
{
if (controller.collisions.slidingDownMaxSlope)
{
velocity.y += controller.collisions.slopeNormal.y * -gravity * Time.deltaTime;
}
else
{
velocity.y = 0;
}
}
if (Input.GetAxis("Horizontal") < 0 && facingRight) Flip();
if (Input.GetAxis("Horizontal") > 0 && !facingRight) Flip();
}
public void SetDirectionalInput(Vector2 input)
{
directionalInput = input;
}
public void OnJumpInputDown()
{
if (wallSliding)
{
if (wallDirX == directionalInput.x)
{
velocity.x = -wallDirX * wallJumpClimb.x;
velocity.y = wallJumpClimb.y;
}
else if (directionalInput.x == 0)
{
velocity.x = -wallDirX * wallJumpOff.x;
velocity.y = wallJumpOff.y;
}
else
{
velocity.x = -wallDirX * wallLeap.x;
velocity.y = wallLeap.y;
}
}
if (controller.collisions.below)
{
if (controller.collisions.slidingDownMaxSlope)
{
if (directionalInput.x != -Mathf.Sign(controller.collisions.slopeNormal.x))
{ // not jumping against max slope
velocity.y = maxJumpVelocity * controller.collisions.slopeNormal.y;
velocity.x = maxJumpVelocity * controller.collisions.slopeNormal.x;
}
}
else
{
velocity.y = maxJumpVelocity;
}
}
}
public void OnJumpInputUp()
{
if (velocity.y > minJumpVelocity)
{
velocity.y = minJumpVelocity;
}
}
void HandleWallSliding()
{
wallDirX = (controller.collisions.left) ? -1 : 1;
wallSliding = false;
if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0)
{
wallSliding = true;
if (velocity.y < -wallSlideSpeedMax)
{
velocity.y = -wallSlideSpeedMax;
}
if (timeToWallUnstick > 0)
{
velocityXSmoothing = 0;
velocity.x = 0;
if (directionalInput.x != wallDirX && directionalInput.x != 0)
{
timeToWallUnstick -= Time.deltaTime;
}
else
{
timeToWallUnstick = wallStickTime;
}
}
else
{
timeToWallUnstick = wallStickTime;
}
}
}
public void Flip()
{
facingRight = !facingRight;
transform.localScale = new Vector3(
transform.localScale.x * -1,
transform.localScale.y,
transform.localScale.z);
}
void CalculateVelocity()
{
float targetVelocityX = directionalInput.x * moveSpeed;
velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne);
velocity.y += gravity * Time.deltaTime;
}
}