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
0
Question by DonGato · Dec 19, 2010 at 05:30 PM · camerajavascriptmovementrigidbodyplayer

Move RigidBody character relative to camera.

I know there are a million other questions like this here, so sorry for being an idiot. Here goes:

My character is a RigidBody ball, and I have this:

( "a < b" would mean that a is the parent of b)

Ball < GameObject < Camera

Ball has a simple movement script attached.

GameObject has a script to stop it from rotating when Ball does.

Camera has a camera movement script.

Everything works right, except that the movement of Ball is not camera relative. Pushing forward always results in movement in one direction, regardless of which way the camera points.

The complicating factor is that I can't simply make the y rotation of Ball equal to that of the Camera, because that would effect the physics (if you hit a wall while spinning the camera you would bounce off differently).

So how do I make movement of my character relative to camera direction without rotating my character on the y axis when the camera turns?

JavaScript please!!!!

My movement script:

var speed : float = 6.0; private var RotDirection : Vector3 = Vector3.zero;

function FixedUpdate () { RotDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); RotDirection *= speed;

 rigidbody.AddTorque (RotDirection); 

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by The_r0nin · Dec 19, 2010 at 05:58 PM

impulseDirection:Vector3 = Camera.main.transform.TransformDirection(Vector3(0,0,10));

That should give you a world-vector for your local camera Z-Axis vector times ten.

Comment
Add comment · Show 3 · 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 DonGato · Dec 20, 2010 at 06:29 AM 0
Share

Where do I apply that script?

BTW I added my movement script to the question, that may help you answer...

avatar image The_r0nin · Dec 20, 2010 at 01:14 PM 0
Share

impulseDirection should be in your rigidbody.AddTorque(). Right now you are adding torque in the world axes rather than on the local axes. You could use AddRelativeTorque ins$$anonymous$$d with the code you already have (I assumed you were using forces). Try both ways...

avatar image DonGato · Dec 20, 2010 at 06:04 PM 0
Share

Ok problem solved...

Basically I ended up using a modified version of the example script under TransformDirection in the scripting reference. Without you I may not have found out about TransformDirection, so Thank You!!!

avatar image
0

Answer by Techsuport · Aug 19, 2011 at 03:13 AM

also you can go..

        transform.forward*10
                 =
 transform.TransformDirection(Vector3(0,0,10)


both give transform relative vectors

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

1 Person is following this question.

avatar image

Related Questions

Making my look script turn smoothly 0 Answers

Set Max Rotation On Weapon Sway 0 Answers

How to turn two scripts on/off with a button 1 Answer

Why is an object's movement jerky, when the camera is also moving by following a different rigidbody? 3 Answers

I need help to make the player go towards where the camera is facing. 1 Answer


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