- Home /
Moving with instantiated projectile
Hi, what Im trying to get done is make my player moves with the instantiated object projectile as if he turned into it to travel for a couple seconds then turns back to normal
Is the game you are working on first person or not? Are you basically trying to get the camera to follow a bullet/missile/etc and then go back to the player or is your game 3rd person?
i would consider it more of a 2.5d side scroller .. i want my character to kinda like travel with the projectile as if he became it and shoot himself forward
Well...? :) If you use a rigidbody to move your character, simply add the appropriate force vector to it. If using a CharacterController, move him with a CoRoutine.
Hi again. Have you solved this issue yet or not? I would be happy to provide some code if need be :).
i couldn't get exactly want i wanted but i used
myRb.AddRelativeForce(Vector3.forward * warpSpeed );
and it get give me like a kinda forward teleport movement which wasn't what i wanted .. but id like to try your code out maybe you have something better :) @TheUltimate$$anonymous$$erbonaut
Answer by TheUltimateKerbonaut · Dec 24, 2016 at 04:55 PM
I think this bit of code bellow might help. Please note some variables you might need to change to your own, like the player GameObject. It basically just makes the player follow the projectile, with an optional offset (you could just set it to 0.0f if you didn't want it).
private bool needToFollowProjectile;
public GameObject player;
private GameObject projectile; // Code will tell what the gameobject is, so no need to make it public.
public float playerProjectileFollowOffsetX;
public float playerProjectileFollowOffsetY;
public float playerProjectileFollowOffsetZ;
public void onProjectileFire() { // Basically, just imagine this is the function or peice of code that executes when projectile is fired.
needToFollowProjectile = true; // We just need to know if we need to make the player follow, thats all.
projectile = instantiatedPrefab; // I imagine when you instantiate the prefab, you have some sort of gameobject you can use as a variable.
}
public void LateUpdate() { // Calls after Update() meaning it is an ideal place to make one gameobject follow another, without any jittering/lag.
if (needToFollowProjectile) {
player.transform.position = new Vector3 (projectile.transform.position.x + playerProjectileFollowOffsetX, projectile.transform.position.y + playerProjectileFollowOffsetY, projectile.transform.position.z + playerProjectileFollowOffsetZ);
}
}
By the way, to find out more about LateUpdate(), visit here. If you need any more help just ask :).
Answer by waittobi · Dec 28, 2016 at 11:38 PM
Sorry for a late reply i've been around family and not around a cpu ... i've been playing around with your code but everything i was trying wasn't working .. maybe if i show u my script that i'm trying to add it too it'll help... this script is attached to my player @TheUltimateKerbonaut ... {
public Rigidbody chakraBlastPrefab;
public Transform spawnPoint;
public float warpSpeed;
Rigidbody myRb;
AudioSource source ;
public AudioClip chakraSound;
void Awake()
{
source = GetComponent<AudioSource> ();
myRb = GetComponent<Rigidbody> ();
}
void Update ()
{
if(Input.GetKeyUp("z"))
{
Shoot ();
}
void Shoot ()
{
Rigidbody chakraBlast = Instantiate(chakraBlastPrefab, spawnPoint.position, Quaternion.identity ) as Rigidbody ;
source.Stop ();
source.PlayOneShot (chakraSound);
chakraBlast.AddForce(spawnPoint.right * 2000);
}
}
}
What is wrong with the code I provided? Any error messages? Have you assigned all the variables in the editor. I apologise for not actually testing the code (was away too, which is also the reason for late reply), will try it out when I can. What actually happens when the code is run? Try the standard Debug.Log() in various places to see if any code isn't executing correctly.
sorry been away..... When i added your code it worked a bit but it always jump my player just back to the same place regardless of my position which was 0,0,0, i want it to follow my projectile @TheUltimate$$anonymous$$erbonaut
So, basically, the character follows the projectile, then doesn't go back to his original position? I didn't include that into the code, you would just have to modify player's transform.position to put him back where he was using a temporary variable that stores original position.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Spawn Ground right next to current ground 1 Answer
How do I setup fixed movement between predetermined tiles? 0 Answers