- Home /
need help with top down shooter,need help with topdown shooting
my shooting is doing wierd stuff
video of whats wrong: https://youtu.be/Nw1M-OdTF_A
player script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playercontrol : MonoBehaviour
{
public Camera cam;
public float speed = .25f;
public GameObject bulletobj;
public float bulletspeed = 10;
public Transform shootpos;
// Update is called once per frame
void Update()
{
float movex = Input.GetAxisRaw("Horizontal");
float movey = Input.GetAxisRaw("Vertical");
transform.position += new Vector3(movex, movey).normalized * Time.deltaTime *speed;
Vector3 mousepos = cam.ScreenToWorldPoint(Input.mousePosition);
float angle = Mathf.Atan2(mousepos.y - transform.position.y, mousepos.x - transform.position.x);
angle *= Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, angle-90);
if (Input.GetButtonDown("Fire1"))
{
GameObject bullet;
//add shooty code
bullet = Instantiate(bulletobj, shootpos.transform.position, shootpos.transform.rotation);
}
}
}
bullet prefab script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bulletmove : MonoBehaviour
{
public float bulletspeed = 100f;
public void Update()
{
// Move the object forward at 1 unit/second.
transform.Translate(transform.up * Time.deltaTime * bulletspeed);
}
}
,so im having a problem with the angle of shooting on a topdown shooter script
video of whats wrong: https://youtu.be/Nw1M-OdTF_A
player script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playercontrol : MonoBehaviour
{
public Camera cam;
public float speed = .25f;
public GameObject bulletobj;
public float bulletspeed = 10;
public Transform shootpos;
// Update is called once per frame
void Update()
{
float movex = Input.GetAxisRaw("Horizontal");
float movey = Input.GetAxisRaw("Vertical");
transform.position += new Vector3(movex, movey).normalized * Time.deltaTime *speed;
Vector3 mousepos = cam.ScreenToWorldPoint(Input.mousePosition);
float angle = Mathf.Atan2(mousepos.y - transform.position.y, mousepos.x - transform.position.x);
angle *= Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, angle-90);
if (Input.GetButtonDown("Fire1"))
{
GameObject bullet;
//add shooty code
bullet = Instantiate(bulletobj, shootpos.transform.position, shootpos.transform.rotation);
}
}
}
bullet prefab script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bulletmove : MonoBehaviour
{
public float bulletspeed = 100f;
public void Update()
{
// Move the object forward at 1 unit/second.
transform.Translate(transform.up * Time.deltaTime * bulletspeed);
}
}
Answer by MUG806 · Feb 15, 2021 at 05:15 PM
Took me a while but I found the mistake! Luckily its a simple one, just hard to spot at a distance. The problem lies here:
// Move the object forward at 1 unit/second.
transform.Translate(transform.up * Time.deltaTime * bulletspeed);
By default, transform.Translate moves the object in its own space, not world space. So you don't want to pass in directions relative to its own direction like "transform.up". To fix this you can do either of the following:
// Pass in Vector3.up, which is an absolute up, rather than a relative up
transform.Translate(Vector3.up * Time.deltaTime * bulletspeed);
//use the optional parameter to tell unity you are giving a world space direction, not local.
transform.Translate(transform.up * Time.deltaTime * bulletspeed, Space.World);
Both are equivalent so use whichever makes the most sense to you!
EDIT: Bonus tip, you might wanna try out this more readable (and trig free!) "look at mouse" solution:
//make sure the mouse pos is on the same depth as the transform
mousepos.z = transform.position.z;
//Look into the screen, but rotate the "up" of the transform to face mousepos
transform.rotation = Quaternion.LookRotation(Vector3.forward,mousepos-transform.position);