This is a pretty basic question, but I don´t realize to do it. My idea is to spawn a projectile in the player´s position and move it forward.
I tried to take the camera´s and the spawned projectile´s location and substract them to create the direction vector.
After that, I take the player´s location and substract the direction vector so now in theory I have the new position forward the player in direction of the camera. Here is my idea in 2D:
I just found that there is a very usefull function called “GetLocalForward()” with this I can make the same process as before, but now the code works also in first person.
Here is it.
ForwardVector:=Rotation.GetLocalForward()
#CAMERA
Camera_LocationX:=Character.GetViewLocation().X
Camera_LocationY:=Character.GetViewLocation().Y
Camera_LocationZ:=Character.GetViewLocation().Z
#PROJECTILE SPAWN
Rocket_LocationX:=Spawned_Rocket.GetTransform().Translation.X
Rocket_LocationY:=Spawned_Rocket.GetTransform().Translation.Y
Rocket_LocationZ:=Spawned_Rocket.GetTransform().Translation.Z
#CALCULATING NEW POSITION
NewPosition_LocationX:=Rocket_LocationX+(ForwardVector.X*1000.0)
NewPosition_LocationY:=Rocket_LocationY+(ForwardVector.Y*1000.0)
NewPosition_LocationZ:=Rocket_LocationZ+(ForwardVector.Z*1000.0)
NewPosition:vector3=vector3{X:=NewPosition_LocationX,Y:=NewPosition_LocationY,Z:=NewPosition_LocationZ}
NewTransform:transform=transform{Translation:=NewPosition,Rotation:=Rotation,Scale:=Scale}
Print("Trying to move rocket")
Spawned_Rocket.MoveTo(NewTransform,5.0)
If you just want it to shoot forward from the player, you can set rotation to match player control rotation. It can go in the projectile’s construction script.
You can use Instigator > Get Control Rotation > Set Actor Rotation
Be sure to set an instigator.
You may want to update the tags if you’re looking for C++. It says Blueprint at the moment.