I'm making a 2D shooting game with unity.
When the enemy hits the bullet, I tried to fly to where I was, but it stopped there.
I want to keep the bullet going on that line.
The following error message occurred while implementing a function.
When an enemy hits a bullet, I try to fly to where I was, but the bullet stops where I was.
Applicable source code`` `c #
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemybullet: MonoBehaviour
{
GameObject Player;
GameObject Enemy;
public float speed = 3.0f;// movement speed]
public Vector3 PlayerPos {get;private set;}
// Start is called before the first frame update
void Start ()
{
// get player object
this.Player = GameObject.Find ("Player");
this.Enemy = GameObject.Find ("Enemy");
Destroy (gameObject, 3.0f);
// Vector3 PlayerPos = this.Player.transform.position;
PlayerPos = this.Player.transform.position;
}
// Update is called once per frame
void Update ()
{
// float step = speed * Time.deltaTime;
//Enemybullet.transform.position = Vector3.MoveTowards (Enemybullet.transform.position, Player.transform.position, step);
float ENEMY_MOVE_SPEED = 0.05f;
// Move in the direction of the player
transform.position = Vector3.MoveTowards (transform.position, PlayerPos, ENEMY_MOVE_SPEED);
}
}
I think Movetowards is the code that goes there, but what do you use instead?
Supplemental information (FW/tool version etc.)unity2019.2.19
-
Answer # 1
-
Answer # 2
MoveTowards will go to that point, so don't pass
I think a constant velocity straight bullet looks like this
Vector3 move; void Update () { transform.position = transform.position + move * Time.deltaTime; } // Usage is like this // Aim (enmey.transform.positon, player.transform.position, 3.0f); void Aim (Vector3 positon, Vector3 target, float speed) { move = (target-position) .normalized * speed; }
With STG, Enemy is not just one, and it is always monotonous to aim only at Player, so
I don't think these values should be retrieved internally
Related articles
- c # - 2d shooting game: enemy bullets head toward the player
- c # - player production in unity (shooting game)
- unity2d - i want to be able to put out bullets of shooting game in 3 directions
- c # - i can't see the bullets fired by the enemy
- unity2d c # lightening hit judgment in shooting games
- c # - rail-type gun shooting such as the house of the dead and the time crisis series (in addition, in fps, the movement of the
- c # - i want to make bullets falling from the tip
- c # - bullets can not be generated from enemies generated by prefab
- c # - player production in unity (shooting game)
- c # - please tell me how to use the additional components of unity's volunteer production
- c # - conversion from enum type to enum type
- c # - missingreferenceexception occurs even though there is a reference
- c # - i want to update the value
- c # - i want the camera to follow the player character in a tps game, but if i make multiple players in multiplayer, the camera
- c # - [unity] even though "has exit time" of animator is specified, it will transition to another state earlier than t
- c # - == becomes false even though the strings are equal
- c # - how to use start () in unity
- c # - in a script? error cs1056: unexpected character'?' is displayed even though there is no
I'm a Unity beginner, so I'll connect to the place until a detailed person comes. . .
Start () calculates "Direction towards player (unit vector)" and stores it in a member variable.
In Update (), I thought it would be better to transform.Translate (vector created by ↑ * Speed * Time.deltaTime).