I am making a simple 2d shooter in unity. I have made a prefab for my weapons and a script to shoot. When I enter the game mode I can see the bullet objects being created, but the bullets don't show on the screen. When I pause it and enter the scene mode they show (most of the time) any idea what could be causing this??
Here is the various settings on my bullet :
Thanks in advance for your help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
//variable to handle what progectile to spawn
public GameObject projectile;
//from where does the bullet coem from
public Transform shotPoint;
//How much time should be taken between shots
public float timeBetweenShots;
//time to shoot another bullet
private float shotTime;
// Update is called once per frame
private void Update()
{
//Callculating the the current mouse position to the current weapon position
//Input.mouse position returns a screenpoint
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = rotation;
//if the left mouse button is pressed
if(Input.GetMouseButton(0)){
//if the current time in the game is greater than the shot time
if (Time.time >= shotTime)
{
//shoot projectile
Instantiate(projectile, shotPoint.position, transform.rotation);
//recalculate shot time current time + time between shots
shotTime = Time.time + timeBetweenShots;
}
}
}
}
Check the order layer if you're by mistake not covering your bullets with the background image. Just make your bullet prefab layer higher then background equivalent.
Moreover, move the background node out of the scope of Player (currently you have it in the Player node), it should be equally set as your Main Camera and Player nodes in the tree. Then check the layering order and make sure order value in background is the lowest of all of your sprites.
Basically you are creating bullets inside of the weapon thats why you are not able to see your bullets. I create simple scene and adding sphere as a bullet then I realized sphere spawning in the gun(for me in the cube). If you will adding force everything will be okay.
Assume the shootPoint is somewhere over the gun barrel,
Instantiate(projectile, shotPoint.position, transform.rotation);
You should give them force,
GameObject projectile= Instantiate (prefab, position, rotation) as GameObject;
rb = projectile.GetComponent<Rigidbody>();
rb.AddForce (direction * speed);
Dont forget to add rigidbody to the bullet
Related
I am trying to build a flappy bird like game and I am trying to spawn enemy birds and gold coins so I have written the C# code and the made the prefabs, but when I run the bird and the coins are not respawning.
This is the respawn code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnPlayer : MonoBehaviour
{
public GameObject GameObjectToSpawn;
private GameObject Clone;
public float timeToSpawn = 4f;
public float FirstSpawn = 10f;
// Update is called once per frame
void Update()
{
FirstSpawn -= Time.deltaTime;
if (FirstSpawn <= 0f)
{
Clone = Instantiate(GameObjectToSpawn, gameObject.transform.localPosition, Quaternion.identity) as GameObject;
FirstSpawn = timeToSpawn;
}
}
}
screenshot of unity:
This where i am respawning the first enemy bird:
From your second screenshot it seems to be spawned but way off the screen! You can still see the tiny little island in the bottom left corner.
You thought seems to be that you have to spawn it in the Canvas pixel space using the spawn point's localPosition. But this is not the case since Instantiate places it into the scene root (without any parent) with absolute world-space position into the scene.
You should rather actually place the spawn point to the absolute world position where the spawn should happen and rather use
Clone = Instantiate(GameObjectToSpawn, transform.position, Quaternion.identity);
Btw no need for the as GameObject since Instantiate already returns the type of the given prefab.
I am using unity 2018.3.5f1 so a few solutions to this problem don't work.
Starting from the Roll-A-Ball demo project, I want to extend the camera script to follow the player, but also keeping track of the direction of movement. Ex: If the ball starts moving to the side, I need the camera to rotate around the player's axis to position behind him, and then start following him.
I have tried making the camera a child to the ball i'm trying to control and i've tried making a script but it won't follow the ball correctly it keeps rotating the camera as well as the ball (i'm using this bit of code and trying to modify it as it's the only one that works)
here is the code I have at the moment:
using UnityEngine;
using System.Collections;
public class CompleteCameraController : MonoBehaviour
{
public GameObject player; //Public variable to store a reference to the player game object
private Vector3 offset; //Private variable to store the offset distance between the player and camera
// Use this for initialization
void Start()
{
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - player.transform.position;
}
// LateUpdate is called after Update each frame
void LateUpdate()
{
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
transform.position = player.transform.position + offset;
}
}
I understand why it is rotating but no reasearch is helping me find out how to lock the camera so it is looking behind the ball at all times
Have you tried adding some restraints to your camera? If it is only supposed to turn to follow the ball, you can freeze the position of 2 axes (x and z), and only let the camera rotate around the y axis of the ball.
You can play around with the restraints on the Inspector, or use GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationX; in the camera script.
(EDIT) Sorry, this is how you would do it if the gameObject has a rigidbody, but the idea is the same if it doesn't have one.
When you're setting the position or rotation of the camera, think about which components of the position/rotation you actually want to change (x,y,z).
Use offset = new Vector3 (offset.x, offset.y, offset.z)and replace anything that shouldn't change with a constant.
Also, when you made the Camera the child of the ball, you could have set it so that every frame, the Camera updates in such a way that it won't roll with the ball. You could do this by putting code in the update method of the camera that sets the component x, y, or z equal to some constant. Whatever axis the ground plane is on is probably the correct choice.
If you want the same camera angle that the camera should follow while following the ball (similar to Temple Run), then try the below code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class CompleteCameraController : MonoBehaviour
{
public Transform Player;
public float distanceFromObject = 3f;
void Update()
{
Vector3 lookOnObject = Player.position - transform.position;
lookOnObject = Player.position - transform.position;
transform.forward = lookOnObject.normalized;
Vector3 playerLastPosition;
playerLastPosition = Player.position - lookOnObject.normalized * distanceFromObject;
playerLastPosition.y = Player.position.y + distanceFromObject / 2;
transform.position = playerLastPosition;
}
When the ball moves left or right, the camera will follow the same angle as the ball moves towards.
I'm making a 2D Block breaker game; the issue is that my ball will slow down after a while, yet there isn't any gravity and the bounciness is at 100% so it should keep all its kinetic energy. I just need the ball to stay at a constant speed.
Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour {
public Paddle paddle;
private Vector3 paddleToBallVector;
private bool Started = false;
void Start () {
paddleToBallVector = this.transform.position
paddle.transform.position;
print(paddleToBallVector);
//this.GetComponent<Rigidbody2D>().drag = 0f;
}
void FixedUpdate () {
if (!Started) {
this.transform.position = paddle.transform.position + paddleToBallVector;
if (Input.GetMouseButtonDown(0))
{
Debug.Log("mouse clicked, Started = " + Started);
Started = true;
this.GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);
}
}
}
}
The issue isn't in the code - it's in the game Gravity settings. You could solve it in one of two ways:
1) change the gravity scale of the ball - go to the ball game object or Prefab, and look at the RigidBody2D component. In there, you have a field called "Gravity Scale", usually set to 1. Changing it to 0.1 will make your ball light and quick. However, this setup isn't ideal for a game with multiple levels.
2) Change the global gravity in the project settings. Go in the Edit Menu to the Project Settings and in the menu choose Physics2D:
In the panel that is opened you usually have a realistic gravity scale of -9.81. Change it to something like -1.
This will make all the objects in your game light, and lessen the hold of gravity for all levels. In a brick-breaker type game when only the ball is loose and thrown around, it makes the most sense.
If you want the ball to have constant velocity, you could set it each frame in Update
myRigidbody.velocity = speed * (myRigidbody.velocity.normalized);
Where myRigidBody is a private RigidBody declared somewhere in your script, and set in Start() like this
myRigidbody = this.GetComponent<Rigidbody>();
Because you dont want to have to call GetComponent every single frame.
Create a physics material 2D. Keep the bounciness at 1 but reduce the friction to zero. Now attach that material to your Ball Collider. Your ball won't lose any speed when colliding now
I am building a 3D maze that is moved by the player. I'm taking a "labyrinth" approach and having a small ball be maneuvered through the maze when the player moves the maze. The problem occurs when the player moves a wall that the ball is currently resting on in the direction of the ball. The ball passes through the wall and stops on the next available wall.
The maze uses a mesh collider and rigidbody and the ball is a sphere collider.
I have drastically increased the physics frame rate to no avail. Considering the complexity of the maze and the potentially large number of maze combinations I really don't want to attach simple colliders to every wall. Any tips, tricks, suggestions, comments, etc. would be appreciated.
Ball Script for continious rotation:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ballGravity : MonoBehaviour {
public Rigidbody m_Rigidbody;
public Vector3 m_EulerAngleVelocity;
// Use this for initialization
void Start () {
m_EulerAngleVelocity = new Vector3 (0, 100, 0);
m_Rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
Quaternion deltaRotation = Quaternion.Euler (m_EulerAngleVelocity * Time.deltaTime);
m_Rigidbody.MoveRotation (m_Rigidbody.rotation * deltaRotation);
}
}
Maze Rotation Script:
using UnityEngine;
using System.Collections;
public class rotObj : MonoBehaviour
{
private float baseAngle = 0.0f;
float rotSpeed = 10;
void OnMouseDown(){
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
baseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) *Mathf.Rad2Deg;
}
void OnMouseDrag(){
//float rotY = Input.GetAxis("Vertical")*rotSpeed*Mathf.Deg2Rad;
//gm.transform.Rotate(Vector3.right, rotY);
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
float ang = Mathf.Atan2(pos.y, pos.x) *Mathf.Rad2Deg - baseAngle;
transform.rotation = Quaternion.AngleAxis(ang, Vector3.forward);
}
}
To get consistent physics state its best to move colliders within FixedUpdate - if you move colliders in a OnMouseXX stage of the pipeline (Consider consulting https://docs.unity3d.com/Manual/ExecutionOrder.html), you risk that the change might be missed next time FixedUpdate is called. I.e. your ball moves through the state of your scene based on how it was before you rotated the maze, and with some unfortunate timings its possible that collisions get missed.
If you already tried with a tighter timestep, consider queueuing the OnMouseDrag change, like store the resulting rotation in a variable, and apply it in FixedUpdate, so it is applied just in case for next physics computation.
you may also consider moving your ball at the same time as you rotate your maze, quickly moving a mesh collider against a much smaller rigidbody is a sure source of trouble. Can you move a camera instead ?
I am having my player constantly drop downwards on the screen and when the player interact with other gameobjects, I want to destroy those gameobjects and want the player to carry on dropping.
But the moment the player hits the other gameobject, the gameobject does gets destroyed but the player stops dropping. Please help advice what I am doing wrong.
//Script attached to player:
//x-axis movement speed
public float playerMoveSpeed = 0.2f;
//how much force to act against the gravity
public float upForce = 9.0f;
//horizontal control
private float move;
// Update is called once per frame
void Update()
{
//player left right x-axis movement control
move = Input.GetAxis("Horizontal") * playerMoveSpeed;
transform.Translate(move, 0, 0);
}
void FixedUpdate()
{
//to fight against the gravity pull, slow it down
rigidbody.AddForce(Vector3.up * upForce);
}
//Script attached to gameObject to be destroyed on contact with player
void OnCollisionEnter(Collision col)
{
//as long as collide with player, kill object
if (col.gameObject.tag == "Player")
{
Destroy(gameObject);
}
}
First one should solve your problem, the second one might make your life easier :)
1.) Mark the objects as "Triggers" so that no real collision happens. That way your player should just fall through and keep its speed. You also need to use OnTriggerEnter instead of OnCollisionEnter
2.) If you dont really need "forces" but just want to move the player constantly you could just turn off gravity and set rigidbody.velocity manually like(i am assuming 2d here):
void FixedUpdate()
{
horizontalVelocity = Input.GetAxis("Horizontal") * playerMoveSpeed;
rigidbody.velocity = new Vector3(horizontalVelocity, verticalVelocity, 0.0f);
}
Just play around with values for verticalVelocity and horizontalVelocity untill it feels right.
Also note that if you move something in Update() you should probably multiply the translation with Time.deltaTime or your player will move faster on higher fps. FixedUpdate is called in fixed time intervalls so you dont need it there(thats why its called Fixed Update).