#Projectiles
1 messages · Page 1 of 1 (latest)
Sorry I hope you don't mind but I created a thread
@ionic hearth It seems to stop the projectiles completely
show your code
public class Projectile : MonoBehaviour
{
public float speed;
private Vector3 target;
private Vector3 direction;
// Start is called before the first frame update
void Start()
{
target = PlayerController.player.transform.position;
direction = target - transform.position;
gameObject.transform.LookAt(target);
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, direction, speed * Time.deltaTime);
}
} ```
replace speed * Time.deltaTime with 30 maybe, and test that
okay
oh your LookAt code should also be in Update
ah I thought I thought doing that would set the projectile to rotate every frame but I think now I have replaced it with the target member it will only have the starting position. Thank you
Okay it now spawns in but doesnt move
are you 2D? or 3D?
i did this yesterday, also made my thing not move
let me find the code
i think it was this
Vector3 vectorToTarget = targetTransform.position - transform.position;
float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
remove the Vector3 line
inside the Mathf.Atan2
do Direction.y, Direction.x
and thats it i think
Thanks. I find unity a bit of a struggle sometimes. Like I program a lot but have only just started using unity
i mean this is just math really i think
yeah though I did this in JS no problem. It is just learning what you can do in unity and how to go about it and what you need to script yourself
@ionic hearth nah it still doesn't work. That is in the start function I need that right
sorry update
public class Projectile : MonoBehaviour
{
public float speed;
private Vector3 target;
private Vector3 direction;
// Start is called before the first frame update
void Start()
{
target = PlayerController.player.transform.position;
direction = target - transform.position;
}
// Update is called once per frame
void Update()
{
gameObject.transform.LookAt(target);
rotateToTarget();
transform.position = Vector3.MoveTowards(transform.position, direction, 30);
}
private void rotateToTarget()
{
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
}
}
remove the LookAt line
a couple years
ahh fair enough I mean it seems pretty good just think it is gonna take a little bit more learning
are you sure it isn't to do with direction just whenever I use that it just seems to not do anything
what is the value of Target
so its the position of a staticObject of the class playerController
so player is just public static PlayerController player
and then I instantiate it in the start function
do this
void Start()
{
target = PlayerController.player.transform.position;
direction = target - transform.position;
Debug.Log(target);
Debug.Log(direction);
}
oh actually maybe before this, do direction.Normalized();
i forgot the normalizing
ahh okay
try normalizing it, if that doesnt work, then do Debug.Log
and remove the LookAtline, you dont need it
direction.Normalize();