Question by
RedLogicP · Jan 25, 2020 at 02:55 AM ·
2dprojectileshooter
Projectiles aren't shooting the direction they're supposed to with transform.up
I'm trying to create a 2D shooter and this is the script I'm using:
public class PlayerController : MonoBehaviour
{
public GameObject snowball;
public GameObject wall;
public float playerSpeed;
void Start()
{
}
// Update is called once per frame
void Update()
{
faceMouse();
moveChar();
if(Input.GetMouseButtonDown(0))
{
Instantiate(snowball, transform.position, transform.rotation);
}
}
void faceMouse()
{
Vector2 direction = getDirectionVector();
transform.up = direction;
}
Vector2 getDirectionVector()
{
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
return new Vector2(mousePosition.x - transform.position.x, mousePosition.y - transform.position.y);
}
void moveChar()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
transform.Translate(new Vector2(moveHorizontal * playerSpeed * Time.deltaTime, moveVertical * playerSpeed * Time.deltaTime), Space.World);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SnowballBase : MonoBehaviour
{
public float throwSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(transform.up*throwSpeed);
}
}
But the objects go off in seemingly random directions that don't follow my mouse at all. Could I get some help?
Comment