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 samaxi · Apr 03, 2014 at 12:05 PM · c#javascriptcollision

Character falls through the ground

Here is my movement script.

When I move the player he falls through the ground.

Please help.

 using UnityEngine;
 using System.Collections;
 
 public class movement : MonoBehaviour {
     
     public RaycastHit hit;
     public Ray ray;
     public Vector3 direction;
     public float moveSpeed = 3f;
     public int rotateSpeed = 75;
     private Transform PlayerTransform;
     
     
     
     // Use this for initialization
     void Start () 
     {
         PlayerTransform = transform;
         direction = PlayerTransform.position;
     }
     
     // Update is called once per frame
     void FixedUpdate () 
         
     {  
         if(Input.GetMouseButton(0))
             
         {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if(Physics.Raycast (ray, out hit, 100))
             {         
                 direction = hit.point;
                 
                 PlayerTransform.position = Vector3.MoveTowards(PlayerTransform.position, hit.point, moveSpeed * Time.deltaTime);
                 Debug.DrawLine(PlayerTransform.position, hit.point, Color.blue, 2);
                 
                 Vector3 targetPoint = hit.point;
 
                 PlayerTransform.position = Vector3.MoveTowards(PlayerTransform.position, hit.point, moveSpeed * Time.deltaTime);
                 targetPoint.y = 0;
             }
             
             
         }
         
     }
 }
Comment
Add comment · Show 3
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 samaxi · Apr 04, 2014 at 01:40 AM 0
Share
 bumb    
avatar image FirePlantGames · Apr 04, 2014 at 02:03 AM 0
Share

are you using 2d colliders on 3d colliders?

avatar image samaxi · Apr 04, 2014 at 09:36 AM 0
Share

3D colliders

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by besttof · Apr 04, 2014 at 10:04 AM

I am not sure why you're altering the position twice when the ray hits, I guess that's just because you're in the middle of trying things? Also, directly changing the position of a physics object (i.e. rigidbody) is asking for trouble, you'd probably be better of using the rigidbody.MovePosition method in this case.

But both those things are probably not why your character is falling trough the floor. The problem (probably) is that the hit.point gives you the point where the ray hits which is likely to be on the floor. If you move your character to that point, you're moving its origin to that point. Chances are that the collider of the character extend below that point, and by placing the origin of the character on the hit.point you're placing it in the floor which makes the collision ignore the floor and thus make it fall trough.

To solve this you could either move the collider (and the mesh etc.) up by giving it an offset, or add an offset to the hit point:

 public Vector3 offset = new Vector3(0, 2, 0);
 
 void FixedUpdate()
 {
   ...
 
   Vector3 targetPoint = hit.point + offset;
   
   ...
 }


Comment
Add comment · Show 2 · 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 samaxi · Apr 04, 2014 at 10:32 AM 0
Share

alt text

Nope :( that didn't fix my problem

untitled.png (4.2 kB)
avatar image besttof · Apr 04, 2014 at 10:54 AM 0
Share

It's a bit hard to see without the colliders or hierarchy visible, but this works for me:

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$ovement : $$anonymous$$onoBehaviour 
 {
     public float moveSpeed = 3f;
     public int rotateSpeed = 75;
 
     Vector3 offset;
 
     void Start()
     {
         offset = new Vector3(0, collider.bounds.extents.y, 0);
     }
 
     void FixedUpdate () 
     {  
         if(Input.Get$$anonymous$$ouseButton(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if(Physics.Raycast (ray, out hit, 100))
             {         
                 Vector3 destination = hit.point + offset;
                 
                 transform.position = Vector3.$$anonymous$$oveTowards(transform.position, destination, moveSpeed * Time.deltaTime);
             }
         }
     }
 }

There are quire a lot of modifications from your script though. For example you never used the targetPoint or direction vaiables.

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

22 People are following this question.

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

Related Questions

Help| Convert a javascript to C# 1 Answer

Randomly Generated Objects 1 Answer

Attack animation Wont Play 1 Answer

What Am I Doing Wrong? Variable Names 3 Answers

Objectives based on object appear. 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