Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Emanrice · Jun 27, 2011 at 04:12 AM · collisionmovementfps

Checking in front of an object before moving

Hi, I have a FPS character script with movement and jumping. Jumping works good, but the movement could be improved. Here's the part of the script in question:

 if (!Physics.Raycast(transform.position,Vector3(0,0,transform.rotation.z),10)){
     var z = Input.GetAxis("Vertical")*speed*Time.deltaTime;
 }

The problem is when you're walking into a wall, the player will have a seizure until finally climbing on top of the wall/floor in front of it. What I'm trying to do to counter this is check for a collision in front of the character before moving. It doesn't work however, it still has the same behavior. I've tried a couple other variants of the code, such as using transform.rotation.z instead of Vector3(blah), but it gives be an wrong argument type error(correct name?). Would anyone mind helping me please?

P.S. Call me lazy, but the player has a rigidbody attached to it to handle the gravity and collisions. P.P.S I'm making this code up from stuff found on the scripting reference, but I don't entirely understand it all though...

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
Best Answer

Answer by Flynn · Jun 27, 2011 at 04:18 AM

Physics.Raycast does not take a rotation, it takes a direction. (1, 0, 0) is a direction that points in the X axis direction, but is not rotated 1 degree on the x axis. (0.5, 0.5, 0) would be a diagonal cross between the X and Y axis directions.

Try replacing your Vector3 with:

transform.TransformDirection(Vector3.forward)

TransformDirection will take a direction in local space relative to the transform, then convert it to global space

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 Dreamblur · Jun 27, 2011 at 04:20 AM 3
Share

Or transform.forward since it's less typing. =P

avatar image Flynn · Jun 27, 2011 at 04:22 AM 0
Share

Never heard of that hehe, good catch

avatar image Emanrice · Jun 27, 2011 at 04:30 AM 0
Share

I really should start remembering that there's a Transform.Direction. Thanks for helping!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to get transform.Translate to work with rigidbodies 2 Answers

How can I make a non-player character (rigidbody) move when they touch the ground? 1 Answer

Quake like movement 1 Answer

Player glitching through wall when sprinting 1 Answer

2D Movement [HELP!] 0 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