Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by SGamerXxX · Aug 07, 2012 at 12:38 AM · gameobjectvelocityaxischaracter controller

[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. alt text

Is this what you wanted?

problem screenshot.png (168.1 kB)
Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Aug 08, 2012 at 01:39 AM 0
Share

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

avatar image SGamerXxX · Aug 08, 2012 at 01:44 AM 0
Share

The camera is a child of the character, and the projectile is fired from the camera's z-axis would that affect it?

avatar image Bunny83 · Aug 08, 2012 at 02:16 AM 0
Share

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?

avatar image SGamerXxX · Aug 08, 2012 at 03:13 AM 0
Share

"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.

avatar image Bunny83 · Aug 08, 2012 at 10:35 AM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SGamerXxX · Aug 07, 2012 at 03:28 AM 0
Share

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.

avatar image aldonaletto · Aug 07, 2012 at 04:03 AM 0
Share

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.

avatar image SGamerXxX · Aug 07, 2012 at 04:45 AM 0
Share

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?

avatar image aldonaletto · Aug 07, 2012 at 01:16 PM 0
Share

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; ...

avatar image SGamerXxX · Aug 07, 2012 at 09:07 PM 0
Share

Edited my question to try to explain the question better.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Velocity relative to Local Axis 1 Answer

Get axis of bullet for rigidbody.velocity 1 Answer

Velocity and Animator change while game is Paused 1 Answer

How does the premade FPSController handle character physics? 1 Answer

How to restrict to one axis 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges