- Home /
[Closed] Find the velocity of the Character Controller
I have an gameObject instantiating at the position of the character controller's camera along its z-axis at speed. Problem is that when I move the character, the gameObject instantiates off its mark. I think I know how to fix it by just adding the gameObject velocity and the character controller velocity together.
Only problem with that is that I can not figure out the character controllers's velocity variable e.g. I have:
gameObject.velocity.x+= ???.velocity.x
Note: gameObject is placeholder for variable name.
Edit: I'm sorry if my question is unclear, I already have a object (Lets call this projectile) instantiating from the z-axis of character's camera.
That fires fine, but what I am unsure about is how to make it so the projectile doesn't stop going directly down z-axis when the character is moving.(momentum changing where projectile is aimed)
I am/was trying to find the character controller's(component) velocity to stop the projectile being moved off course.
Edit 2:
Screenshot to try to help clear things up.
Is this what you wanted?
Once fired, the projectile should follow its initial direction, not being affected by the character movement. Is the instantiated projectile being childed to the character? This could cause this problem
The camera is a child of the character, and the projectile is fired from the camera's z-axis would that affect it?
When you add the characters velocity to the projectiles velocity you will get a strange behaviour. If you move sideways for example, the bullet will fly diagonally so it's always in the center of the players view.
The more natural behaviour would be to just move your projectile forward. It will always spawn at the correct position. The spawning / instantiating happens within one frame. From this moment on the bullet should move on it's own.
Can you describe your problem a bit more in detail? What do you mean by "off it's mark"? Any way to provide a screenshot / video / webplayer sample that shows the problem?
"By off its mark" I mean the crosshair that is in the centre the screen, for example if the character is moving along the x axis the projectile will be slightly to the left or right of the crosshair or if the character is moving along the z axis the projectile will fire slightly above or below the crosshair.
Though when the character is still the projectile will fire dead centre of the crosshair.
I still don't understand what's your desired behaviour. When you shoot and step to the side, you will leave the original line-of-sight. The bullet will move along it's path. The player and the bullet are independent from each other. Again, if you add the player speed to the start velocity the bullet will fly diagonally. When you shoot while you're moving left and then move right the bullet will fly way off to the left.
Again, a screenshot or video would be great. Aldo showed you a way to calculate the current movement.
Answer by aldonaletto · Aug 07, 2012 at 12:59 AM
The property CharacterController.velocity returns the character velocity, but it's based in the actual distance the character moved from last Move or SimpleMove - which may return some unexpected values. A more reliable alternative is to measure the character speed at Update:
var player: Transform; // drag the character here var playerVel: Vector3; private var lastPos: Vector3;
function Start(){ lastPos = player.position; // initialize lastPos }
function Update(){ // calculate displacement since last Update: var moved = player.position - lastPos; // update lastPos: lastPos = player.position; // calculate the velocity: playerVel = moved / Time.deltaTime; } Notice that playerVel is a Vector3, thus it holds the speeds along the axes X, Y and Z in its x, y and z components. Usually you should add playerVel to the object's velocity in order to keep it constant relative to the player, but you can add only the X component, if this makes more sense in your case.
This does not seem to be working, if anything it seems to be go more off its mark. Not sure I quite understand what your code is doing as well... I mean from what i can tell is player.position is getting subtracted from itself? So your trying to zero the player.position? and why lastPos defined twice?.
On another note, yes I am trying for all 3 axes eventually.
The idea is to measure the distance traveled each Update and divide it by the time elapsed (Time.deltaTime) to get the current velocity - see the comments I've added to the answer.
I am still confused to why this is not work though I think I understand you code better, I have your code and I am right I assu$$anonymous$$g that the code excerpt from before, now is meant to be like this: gameObject.velocity.x+= playerVel.x;
I am confused as why my problem is seem worse than before and wondering if this is the right approach to get the instantiated gameObject to work with the moving origin gameObject. Or is there another to calculate the player moved speed and the created initial speed together?
I suspect that the whole solution is wrong, but I could not understand exactly what you wanna do nor what is wrong. If you want to instantiate some object at a fixed distance ahead of the player, just do something like this:
var distance: float = 5; var prefab: GameObject;
... var obj: GameObject = Instantiate(prefab, transform.position + distance transform.forward, transform.rotation); // if the object is a rigidbody and must move in the forward direction: obj.rigidbody.velocity = transform.forward speed; ...
Edited my question to try to explain the question better.