- Home /
Why are my bullets acting as being deflected by the camera borders collider?
I have set up a very easy script to spawn bullets on a button press. The firing point is simply a transform attached to my player, basically in the center of a spaceship. (NB: Bullet is just an abstract class i use to manage different bullet types)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletA_Basic : Bullet
{
private void Update()
{
}
public override void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, firingPoint);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firingPoint.up * bulletForce, ForceMode2D.Impulse);
}
public override void SpecialShoot(int n)
{
StartCoroutine(Special(n));
}
public IEnumerator Special(int n)
{
for (int i = 0; i < n; i++)
{
Shoot();
yield return new WaitForSeconds(0.1f);
}
}
}
I also set up on my player object a collider for the camera borders, so that the ship never crosses out of the screen since the camera is static.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScreenCollider : MonoBehaviour
{
public Camera MainCamera;
private Vector2 screenCollider;
private float objectWidth;
private float objectHeight;
void Start()
{
/*This projects the point at the top-right edge of the screen on worldspace, using a vector that starts from the camera.
*The result is a new 2d vector with origin on the center of the screen (if camera is in center),
*which means the boundaries have positive values on the right side and negative values on the left side */
screenCollider = MainCamera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, MainCamera.nearClipPlane));
//This gets the boundaries of the sprite, starting from the center of the box
objectHeight = transform.GetComponent<SpriteRenderer>().bounds.extents.y;
objectWidth = transform.GetComponent<SpriteRenderer>().bounds.extents.x;
}
// LateUpdate is called once per frame, after all Update functions have been called.
void LateUpdate()
{
//Takes the x and y position of the object and makes it impossible to go over the boundary values.
//NB: Ortographic and perspective cameras have inverted axis, so the - sign must be swapped between x and y
Vector3 objectPosition = transform.position;
objectPosition.x = Mathf.Clamp(objectPosition.x, -screenCollider.x + objectWidth, screenCollider.x - objectWidth);
objectPosition.y = Mathf.Clamp(objectPosition.y, -screenCollider.y + objectHeight, screenCollider.y - objectHeight);
transform.position = objectPosition;
}
}
All of this is working great, except... if i move to the left or right edge of the screen and keep the movement key pressed and shoot at the same time, the bullet is "deflected" towards the middle of the screen, as if the player actually kept moving (i.e. changed coordinates) and the camera followed. Pictures are better than words:
On the edge while idle (shooting normally):
On the edge while pressing the key to move left (shooting is deflected as if the ship was actually moving):
Maybe it's something with my movement script (below)?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 7f;
public Rigidbody2D ship; //Could make rigidbody private as well since it only needs to refer to the ship. Whatever
private Vector2 movement;
private Animator animator;
private void Start()
{
animator = GetComponent<Animator>(); //This must be done because animator is private, while rigidbody is not!
}
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxis("Horizontal");
movement.y = Input.GetAxis("Vertical");
if (movement.x != 0)
{
animator.SetFloat("Direction", movement.x); //This is just making the ship tilt
}
}
private void FixedUpdate()
{
ship.MovePosition(ship.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
Your answer
Follow this Question
Related Questions
Moving Camera With 2 Players 3 Answers
Why isn't my move on collision script working? 0 Answers
Predict Collision 0 Answers
Camera up and down on TouchField 1 Answer