#Projectiles

1 messages · Page 1 of 1 (latest)

empty trellis
#

Sorry I hope you don't mind but I created a thread

#

@ionic hearth It seems to stop the projectiles completely

ionic hearth
#

show your code

empty trellis
#
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);
        

    }
} ```
ionic hearth
#

replace speed * Time.deltaTime with 30 maybe, and test that

empty trellis
#

okay

ionic hearth
#

oh your LookAt code should also be in Update

empty trellis
#

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

empty trellis
ionic hearth
#

are you 2D? or 3D?

empty trellis
#

2d

#

do i need to add like lookAt2D or something like that

ionic hearth
#

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

empty trellis
#

Thanks. I find unity a bit of a struggle sometimes. Like I program a lot but have only just started using unity

ionic hearth
#

i mean this is just math really i think

empty trellis
#

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

ionic hearth
#

yes

#

show the updated code

empty trellis
#
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);
    }
}
ionic hearth
#

remove the LookAt line

empty trellis
#

no that doesn't work

#

how long have you been using unity?

ionic hearth
#

a couple years

empty trellis
#

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

ionic hearth
#

what is the value of Target

empty trellis
#

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

ionic hearth
#

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

empty trellis
#

ahh okay

ionic hearth
#

try normalizing it, if that doesnt work, then do Debug.Log

#

and remove the LookAtline, you dont need it

empty trellis
#

okay it says Vector3 doesn't contain that method Normalize

#

Normalized*

ionic hearth
#

direction.Normalize();

empty trellis