- Home /
Question by
ianbo · Jul 12, 2016 at 08:07 AM ·
scripting problemrigidbodyvelocityspace shootershot
Space Shooter tutorial shot velocity issues
I am new to Unity, and I was trying to use what I know to make the shot speed depend on the speed of the player, such that if they were fired from the player when moving backwards, they'd move at the shot speed MINUS the player speed (PLUS if moving forward). Below is the code I used in the 'Mover' script:
I made sure to reference the player GameObject to the actual player, and my shot speed was set to 20. However, when I ran the game, my shots were now stationary from wherever I fired them. Does anybody know where this comes from?
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour {
private Rigidbody rb;
public float speed;
public GameObject player;
private Rigidbody rbPlayer;
void Start ()
{
rb = GetComponent<Rigidbody> ();
rbPlayer = player.GetComponent<Rigidbody>();
rb.velocity = transform.forward * speed - rbPlayer.velocity;
}
}
Comment
Your answer
Follow this Question
Related Questions
What is wrong with this rigidbody? 2 Answers
Unity autojump script glitch 1 Answer
Stoping Velocity Breaks Script [Solved] 0 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers