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
1
Question by Ebil · Dec 26, 2011 at 10:22 PM · colliderplayershootbullets

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.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Dec 26, 2011 at 11:03 PM

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.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to detect a collider in a particle? 4 Answers

Have script detect which collider 2 Answers

Making a bullet invinceble 0 Answers

Raycast shooting problems 1 Answer

Restart sound when re-entering trigger? 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