Home>
I want to fly a bullet straight, but when I hit the bullet in a normal state, it will fly straight, but when jumping, it will fly diagonally.
What amendments will make you fly straight at any time?
** Jump processing **
if (isJump == false&&gameClear == false&&WaitStop == false)
{
// Jump in space.
if (jumpCount<MAX_JUMP_COUNT&&(Input.GetKeyDown ("space") || Input.GetKeyDown ("joystick button 0")))
{
isJump = true;
anim.SetBool ("Charge", false);
anim.SetTrigger ("Jump");
}
}
float velY = rigidbody2D.velocity.y;
bool isJumping = velY>0.1f? true: false;
bool isFalling = velY<-0.1f? true: false;
anim.SetBool ("isJumping", isJumping);
anim.SetBool ("isFalling", isFalling);
if (isJump)
{
// The second jump after clearing the speed will behave the same as the first jump.
rigidbody2D.velocity = Vector2.zero;
// Jump.
rigidbody2D.AddForce (Vector2.up * force);
// Count the number of jumps.
jumpCount ++;
// Allow jumps.
isJump = false;
// anim.SetBool ("Jump", true);
}
Code
** Add this action to the shooting animation **
void Bullet () // generate a bullet
{
Instantiate (bullet, transform.position + new Vector3 (0f, 1.2f, 0f), transform.rotation);
}
Code
** Scripts added to the generated bullet prefab **
using UnityEngine;
using System.Collections;
public class BulletScript: MonoBehaviour
{
private GameObject player;
private int speed = 10;
void Start ()
{
player = GameObject.FindWithTag ("UnityChan");// Get Unity-chan object
Rigidbody2D rigidbody2D = GetComponent<Rigidbody2D>();// Get rigidbody2D component
// Shoot the bullet in the direction of Unity
rigidbody2D.velocity = new Vector2 (speed * player.transform.localScale.x, 0);
// Align image orientation with Unity
Vector2 temp = transform.localScale;
temp.x = player.transform.localScale.x;
transform.localScale = temp;
// 5 seconds later
Destroy (gameObject, 5);
}
// ********** Start ********** //
void OnTriggerEnter2D (Collider2D col)
{
if (col.gameObject.tag == "Enemy") // Delete bullets when colliding with Enemy
{
Destroy (gameObject);
}
}
private void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.tag == "Enemy") // Deletes bullet if it collides with Enemy
{
Destroy (gameObject);
}
}
// ********** End ********** //
}
Code
-
Answer # 1
Related articles
- unity3d how to shorten code that is too long
- c# - how to use unity initialization code
- unity - i want to make the bullets fired by the enemy fire laterally
- unity - the enemy's bullets on the moving scaffold do not fly + the first bullet flies and disappears without going
- c # - how to use dlls in unity
- unity - the player's bullets fly back, the direction does not change
- How to use Unity timestamp
- unity - multiple bullets will be fired with one touch of the "attack" button
- unity - sometimes only bullets are ejected without playing animation or se
- unity - how to simplify (simplify) sequences
- how to run unity on android
- unity - how to use invoke
- unity - i want to fly in the direction of the arrow
- unity - how to end executeineditmode
- unity2d - launch of unity magic bullets, etc
- c# - how to use unity namespace
- how to use jag array in unity
- unity - how to open the setting file in the vfx lwrp environment
- how to get the size of an object in unity
- how to use sqlite in unity
Related questions
- c # - player production in unity (shooting game)
- a question about unity please tell me how to display animatorsettrigger in onclick() of button
- [unity] i want to make a page that can be swiped vertically
- c# - unity) video shooting function in ios application
- c# - i want to make a unity texture fade
- [unity] i want to determine in what order multiple collision detection objects collided
- c# - i want to change the specified value of the y coordinate to stop each time one object is generated
- c# - unity2d script shouldn't be placed, but could not load symbol is displayed
- c# - i am making a shooting game
- [unity][c#] i can only jump at the edge of the ground
There seemed to be no problem with the code, so I actually tried it.
As long as you test here, it will be ejected straight to the side without a problem even during jumping.
Is there a problem elsewhere?
Test video
http://153.122.22.14/224348.mp4
Please check for the following problems.
・ The bullet and the operating character's corridor are in slight contact with each other, and vertical inertia is added
・ There is a problem with another omitted code
・ There is a problem with the omitted component settings