Adding movement speed of Player to

Hi everyone,

I think i got a relatively simple question, but dunno how to solve it myself.
My Player is colliding with his own bullets (not bullet, its magic, but to simplify lets say bullets).
I want to add now the own playerspeed to the bullet code, but it is in another script.
So how to get the actual speed of a simple object (the player / a cube whatever) and add it to this code:

bulletCreate.rigidbody.AddForce(transform.forward * bulletSpeed);

Thats the code that let the bullets push forward.
Hope someone knows that problem.

I suspect that this isn’t causing your problem, because the bullet/magic probably is faster than the player; I suspect that the bullet/magic is being instantiated too near, what causes the collision; if this is true, create a spawn point (child empty object) at a safe distance from the player and instantiate the bullet/magic using the spawn point position and rotation.

Anyway, if you want to get the player velocity from other scripts, it’s better to measure the player speed yourself. The script below (let’s call it MeasureVelocity.js) can be attached to the player:

public var velocity: Vector3 = Vector3.zero;
private var lastPos: Vector3;

function Start(){
  lastPos = transform.position;
}

function Update(){
  velocity = (transform.position - lastPos)/Time.deltaTime;
  lastPos = transform.position;
}

This works for any object, not only CharacterControllers - just add this script to it.

The second problem is how to access the variable velocity from other scripts: you should have a variable referencing the player (drag the player to it, or find the player at Start), get the script MeasureVelocity and read the velocity variable:

var player: Transform; //<- drag the player here in the Inspector
private var velScript: MeasureVelocity;

function GetVelocity(): Vector3 {
  // get the script reference only once, and let it stored in velScript 
  if (!velScript) velScript = player.GetComponent(MeasureVelocity);
  return velScript.velocity;
}

You can add the code above to the shooting script, and call GetVelocity to get the player velocity as a Vector3.