- Home /
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;
}
}
}
}
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;
...
}
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
Follow this Question
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