How to move and animate projectiles

what does youre projectile.cs script look like?
it should be something like this:

public class Projectile : MonoBehaviour {

    public float speed, damage;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        transform.Translate(Vector3.right * speed * Time.deltaTime);
	}

  
}

Hey Alex,

Just so you know <code> isn’t a supported tag.


See also;

That is exactly what I have.

Can you post up your code?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Projectile : MonoBehaviour {

    public float speed, damage;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        transform.Translate(Vector3.right * speed * Time.deltaTime);
	}
}
1 Like

I can reproduce this behaviour by unticking the tick against the Projectile script component on the Courgette prefab for instance. The courgette will appear and then just spin in one place.

The reason I asked you to post the code was just in case you had a lower-case U in Update etc, often case-sensitivity is over-looked.

So, can you confirmed that the GameObject you are testing has the Project.cs script attached to it.

I can also reproduce this behaviour by not setting a Speed for the projectile in the Inspector, can you confirm that the instance of the GameObject you are testing has this specified also.


See also;

Yes. I have tick against Projectile script component. My projectiles are moving, but not smooth as in the tutorial. Here is the video of it:

I set speed to 1 for axe and 2 for zhuccini.

Hi,

You have rotation going on on the Z axis, note the numbers changing in the video.

Correct. It is changing from 0 to -359. Same in the tutorial.

hmm… Sorry, been a while since I looked at Glitch Garden in detail.

Could you zip up the project files so I can take a loom at this end?

The usual issue is the clamped auto / auto thing, but you said when you changed that they didn’t move at all.

Sounds like perhaps more than one issue.

It looks like an animation issue. When I disable animation, both projectiles are moving smoothly forward. With animation enabled, I noticed that there is a stop at the very end of the rotation. If I can get rid of it, it would be smooth.

…and you definitely don’t have any extra key frames? (zoom right out etc).

… and changing clamped auto to just auto stops the horizontal movement of the projectile completely, correct?

I do not have any extra key frames.
Animation issue

Changing from clamped auto to just auto didn’t stop horizontal movement for zucchini, but did stop it for axe.

1 Like

Changing from clamped auto to just auto didn’t stop horizontal movement for zucchini, but did stop it for axe.

There has to be something else going on there then as well, you would expect the same behaviour for both surely, as the animation should be effectively the same, e.g. just rotate.

If you zip up the project and make it the link available here I will happily take a look to see if I can spot anything.

Hi Rob,
Here is the file.

Strange. It uploaded file 100%, but no link to it here. I tried several times too. Is there another place I can upload it to?

How big is the zip file? I’m guessing for Glitch Garden it’s over 10mb (forum max)?

You can use Google Drive or DropBox or similar and just paste the link to the zip file.

It is about 140 MB
Try this one.

https://drive.google.com/file/d/12HTP4wJEL_8cS07ncmLq0YGA9d7MZpIZ/view?usp=sharing

Hi,

I think I can explain the difference between your zucchini and your axe.


If you open scene 02 Level_1 and select the zucchini, look at the details in the Animation;

image

Now select the axe, and look at the details;

image

Note how for the zucchini you have applied the rotation to the body child GameObject, but for the axe you have applied it to the main axe GameObject. You get a hint in the Inspector also;


So, the solution for your issue is;

  • add a new property to the axe animation for the Body / Rotation
    image

  • set the last keyframe to have a value of -359 on the Z axis
    image

  • remove the Axe: Rotation property
    image

    image

  • don’t forget to stop the animation recording

Now, run your scene again and you’ll see that the axe now moves across the screen.

However, it’s still not quite right, and that’s because of the clamped auto / auto issue, so we need to fix that too.


  • select the first and last keyframes

  • right-click and change the tangents from clamped auto to auto;
    image

Run the scene again, and now they both rotate and move as you would want.


On a related note, check the sort order on the projectiles, defenders and attackers also - the axe currently goes behind your croc.

Hope this helps. :slight_smile:

Rob,
I got it fixed.

Thanks a lot for your help.

1 Like

Privacy & Terms