Enemy shooting at player position - c#

I'm creating a simple Bullet Hell game and I'm testing some things out. I want the enemy bullets to shoot towards the player as soon as he fires.
I tried a bunch of Quaternion methods including LookTowards, FromToRotation, AngleAxis, (etc...) and none seemed to work.
void Start () {
...
StartCoroutine("Shooting");
}
IEnumerator Shooting()
{
while(dead != true)
{
Vector3 position = new Vector3(rb.position.x, rb.position.y - 5f, 0);
Instantiate(Ebullet, position, Quaternion.Euler(0, 0, player.transform.position.x));
yield return new WaitForSeconds(t);
}
}
I expected the bullets to move directly to the player's last known position, instead the enemy shoots at a wrong angle and everytime the player moves, the angle changes according to the the player's left or right movement.

Since you are making a 2D game, instead of:
Instantiate(Ebullet, position, Quaternion.Euler(0, 0, player.transform.position.x);
Try:
Vector3 direction = player.transform.position-transform.position;
direction .Normalize();
float rotation = Mathf.Atan2(direction .y, direction .x) * Mathf.Rad2Deg;
Instantiate(Ebullet, position, Quaternion.Euler(0f, 0f, rotation - 90));

Related

Adapting player behaviour to AI in unity

I have designed an attack method for a player that is full functional, however I am new to AI and have no idea as to where I would begin when it comes to adapting this into a state for an FSM.
protected void UpdateAttackState()
{
// check for input
float rot = transform.localEulerAngles.y + rotationSpeed * Time.deltaTime * Input.GetAxis("Horizontal");
Vector3 fwd = transform.forward * moveSpeed * Time.deltaTime * Input.GetAxis("Vertical");
// Tank Chassis is rigidbody, use MoveRotation and MovePosition
GetComponent<Rigidbody>().MoveRotation(Quaternion.AngleAxis(rot, Vector3.up));
GetComponent<Rigidbody>().MovePosition(_rigidbody.position + fwd);
if (turret) {
Plane playerPlane = new Plane(Vector3.up, transform.position + new Vector3(0, 0, 0));
// Generate a ray from the cursor position
Ray RayCast = Camera.main.ScreenPointToRay(Input.mousePosition);
// Determine the point where the cursor ray intersects the plane.
float HitDist = 0;
// If the ray is parallel to the plane, Raycast will return false.
if (playerPlane.Raycast(RayCast, out HitDist))
{
// Get the point along the ray that hits the calculated distance.
Vector3 RayHitPoint = RayCast.GetPoint(HitDist);
Quaternion targetRotation = Quaternion.LookRotation(RayHitPoint - transform.position);
turret.transform.rotation = Quaternion.Slerp(turret.transform.rotation, targetRotation, Time.deltaTime* turretRotSpeed);
}
}
if(Input.GetButton("Fire1"))
{
if (elapsedTime >= shootRate)
{
//Reset the time
elapsedTime = 0.0f;
//Also Instantiate over the PhotonNetwork
if ((bulletSpawnPoint) & (bullet))
Instantiate(bullet, bulletSpawnPoint.transform.position, bulletSpawnPoint.transform.rotation);
}
}
// Update the time
elapsedTime += Time.deltaTime;
}
This is completely dependent on how you want your state machine to work. An example of this would be the AI trying to seek and destroy other players.
Seeking State: AI would move around the map with a detection zone (collider) that would change state on trigger enter. This would then save a position of the detection's generalized location which would be used to move and rotate the AI tank towards that position.
Firing State: AI enters a new detection zone (collider) within firing range, turret will move to aim at target. AI will also move around the map to keep firing range at a maximum while also keeping track of the last known position of the enemy if they happen to go out of range.
Detecting Fire: Player or other AI fires their weapon this could increase the detection radius resulting in a seeking state being called.
After those functions are figured out, place a switch or something similar within the Update() to allow everything within the AI to run alongside Unity's functions.

Trying to make the player rotate perpendicular to the model's meshes. Unity

I'm currently working on a school game project where i need to have the player climb walls and pick up stuff. I am currently stuck in figuring out how to make the player rotate perpendicular to where the model's faces are while he/she moves towards a corner of a wall as shown below.
Something like this:
I searched for some methods to achieve this but i haven't found anything concrete so far.
Edit: I tried Reasurria's method and it kinda works.
if (Physics.Raycast (transform.position, -transform.up, out hit) && hit.collider.GetComponent<WallModifier> ()) {
transform.rotation = Quaternion.LookRotation (hit.normal, Vector3.right);
//Physics.gravity = hit.normal * -10.0f;
}
My player does indeed rotate correctly pependicular to the slope/wall but then my camera code completly breaks when trying to actually climb the wall. This shows how it looks like in first person and this shows how it looks like in the scene view.
While very funny, it is not obviously desired to have such effect.
Here is the camera code:
void Update () {
if (Input.GetKeyDown (KeyCode.Escape) || toggle && Input.GetMouseButtonDown (0)) {
toggle = !toggle;
}
if (!toggle && Application.isFocused) {
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
mouseLook.y = Mathf.Clamp (mouseLook.y, minClamp, maxClamp);
transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, (Vector3.right + transform.right).normalized);
character.transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up.normalized);
if (rotateModel && antModel)
antModel.transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, (Vector3.right + transform.right).normalized) * Quaternion.Euler (0, 90, 0);
} else {
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
Application.runInBackground = false;
}
}
This code was taken by this person.
Do you mean perpendicular? In other words, rotate along x axis with -90 degree.
transform.Rotate(new Vector3(-90, 0, 0));
If I understand correctly, you may want to use Raycast.normal. So you would raycast from your player position with a direction equal to your player's local Vector3.down (player.transform.up * -1.0f).
You can use Quaternion.LookAt(surfaceNormal) to align your players 'up' vector with the normal of the surface under the player. This may involve some 90 degree offset to make the two 'up' vectors match.
Depending on your goal you may also want to set Physics.gravity to surfaceNormal * -10.0f or implement some custom gravity solution if the new surface will function as ground.
You can add empty GameObject with attached collider, than you can use OnTriggerEnter to rotate your Player. Add flag and on every odd enter rotate in one side and on every even enter rotate to opposite side as shingo said
Example

Unity 3D - rotating gameobject without rotating axis

Hope my title summarises my problem. I have a rocket on a 2d game that only moves horizontally across the screen. I want it to rotate towards the players finger (the direction of movement), but cannot find a way to rotate the object without rotating the whole axis it moves on. I simply need it to seem like it has turned, but it should keep moving along the x. How can I go about this?
void Start () {
//scoreT = GetComponent<TextMeshProUGUI> ();
gameSpeed = 1;
score = 0;
Rigidbody2D rb2d = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
float MoveHorizontal = Input.GetAxis ("Horizontal");
Vector2 Movement = new Vector2 (MoveHorizontal, 0.0f);
rb2d.rotation = Quaternion.Euler (0.0f, 0, 0f, rb2d.velocity.x * -tilt);
transform.Translate (MoveHorizontal * speed, 0, 0);
One thing you can do is to modify rigidbody.rotation of your rocket rocket to make it tilt, when it moves, to one direction or to another. For example:
float tilt - 0.3f;
//In case you prefer the rotation in another axis you just need to modify the position of the rigidbody.velocity.x * -tilt
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
Since you didn't add any code I am not sure how you are moving your rocket, so I will post a generic code you will need to adapt depending on your own project:
public class PlayerController : MonoBehaviour
{
public float speed;
//The tild factor
public float tilt;
//The limit within the spaceship can move
public Boundary boundary;
void FixedUpdate ()
{
//You will need to capture the screen touchs of the user for the inputs
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
//Applying the movement to the GameObject
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
//To ensure the GameObject doesnt move outside of the game boundary
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
//Here is where you apply the rotation
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
As an aside, you are doing a space 2D game you may be interested in checking this tutorial:
https://unity3d.com/learn/tutorials/s/space-shooter-tutorial
What you want is to move your object in global/world space. As it seems the movement of your rocket is currently happening within local space. So when you rotate the rocket it's local coordinates are rotated as well. The world space coordinates are fixed and will never rotate when you change your rocket.
Here is another explanation at that.
You can also have a look at Transform.localPosition and Transform.position and see how your rocket behaves when using one or the other.
You could make the sprite/renderer a child of the GameObject that is your rocket.
Then you can freely rotate the sprite/renderer around without changing the rotation you move the parent GameObject.
This is a hacky solution but it achieves the desired result.
You must move your object relative to the world and rotate locally.
So use Transform.position to move your object and Transform.LocalRotation to rotate it.
Or you can put the part that must rotate as a children of the object that translate, and rotate the children.

Player rotation and camera rotation

i want the player to look into the direction, the camera is looking.
The camera follows the player (3rd person game style).
I've tried it with
transform.localRotation = new Quaternion(transform.localRotation.x,
cameraMain.transform.localRotation.y,
transform.localRotation.z,
transform.localRotation.w);
but it doesn't work.
Sometimes the player starts rotating the other direction.
the following code will make the object (specified in the parameter) face in the direction of where the main camera is looking:
public void lookInDirectionOfCamera(Transform object) {
RayCastHit hit;
if (Physics.raycast(cameraMain.transform.position, cameraMain.transform.forward, out hit)) {
object.forward = hit.point - object.position;
}else { //if the raycast didnt hit anything, make the object face 100 units in front of the camera
Vector3 point = Camera.main.transform.position + Camera.main.transform.forward * 100;
object.forward = point - object.position;
}
}
This will make the player face the point that is forward to the camera. If you just want them to have the same rotation in the y-axis don't use Quaternions!
Instead, you can just do it with Euler angles:
transform.eulerAngles = new Vector3(transform.eulerAngles.x,
cameraMain.transform.eulerAngles.y,
transform.eulerAngles.y);
The reason not to use transform.localRotation is because that is a Quaternion. The y component in a Quaternion is not the same as the y-axis in a Euler angle (what you are used to seeing), Quaternions are very confusing so you should almost never set individual values in them. If you want to edit them only use the built-in methods.
Get the direction the camera is looking with cameraMain.transform.forward, make a copy with a y value of zero, then use Quaternion.SetLookRotation to look in that direction with the global up direction.:
Vector3 cameraDirection = cameraMain.transform.forward;
Vector3 characterLookDirection = new Vector3(cameraDirection.x,
0f,
cameraDirection.z);
Quaternion newRotation = new Quaternion();
newRotation.SetLookRotation(characterLookDirection, Vector3.up);
transform.rotation = newRotation;

Unity3D - Why doesn't my player rotate properly?

I'm building a top down shooter and so I have my camera above my player and the map. Here's the code I've written in the player controller script for movement:
public class playerMovement : MonoBehaviour {
public float speed;
private Camera mainCamera;
void Start () {
mainCamera = FindObjectOfType<Camera>();
}
// Update is called once per frame
void Update () {
// player movement
transform.Translate(speed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, speed * Input.GetAxis("Vertical") * Time.deltaTime);
// Camera Ray casting
Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLength;
if (groundPlane.Raycast(cameraRay, out rayLength)) {
Vector3 look = cameraRay.GetPoint(rayLength);
Debug.DrawLine(cameraRay.origin, look, Color.red);
transform.LookAt(new Vector3(look.x, transform.position.y, look.z));
}
}
}
I want to be able to move the player using the WASD keys and also rotate following the direction on where the mouse is, however I don't want the rotation of the player to change the direction of the keys, I need the player to move forwards if the W key is pressed no matter which way the player is facing.
However for some reason my code makes the player move forwards depending on which way it is facing which I don't want.
How can I fix this?
The problem is that your transform.Translate call is in "self" space. Forward, backward, left, right are all relative to the direction the transform is facing. That is why your player is moving relative to the facing direction.
If you want to translate relative to "global" or "world" space, you have to add an additional parameter.
// player movement
transform.Translate(speed * Input.GetAxis("Horizontal") * Time.deltaTime,
0f,
speed * Input.GetAxis("Vertical") * Time.deltaTime,
Space.World);
Note the Space.World parameter at the end, to set the world coordinate system.
You can find more in the Unity docs here: https://docs.unity3d.com/ScriptReference/Transform.Translate.html
You need to look at the difference between local and global coordinate systems.
Right now your WASD keys are moving the player character according to global coordinates, and you want the WASD movement to be dependant on the player's orientation so you need to use a local coordinate system.
http://wiki.unity3d.com/index.php?title=Converting_Between_Coordinate_Systems

Categories