- Home /
Question by
Ochreous · May 09, 2014 at 01:48 AM ·
c#oncontrollercolliderhit
C# OnControllerColliderHit Detection Problems
I'm trying to make a drag Gameobject Script with OnControllerColliderHit. I want to make it so that the ControllerColliderHit variable hit is always following your mouse cursor while the scripting gameobject with a character controller is colliding with it. I'm not sure what's happening but hit either doesn't do anything or zooms across the screen. Any idea what is wrong?
using UnityEngine;
using System.Collections;
public class DragCollidedGameObject : MonoBehaviour
{
private bool canMove = false;
void OnControllerColliderHit(ControllerColliderHit hit)
{
if(!hit.gameObject.rigidbody){
if(hit.gameObject.tag == "Enemy")
{
hit.gameObject.AddComponent<Rigidbody>();
}
}
if (Input.GetMouseButton(0))
{
if(hit.gameObject.tag == "Enemy")
{
canMove = true;
hit.gameObject.rigidbody.freezeRotation = true;
hit.gameObject.rigidbody.useGravity = false;
Vector3 move = Camera.main.ScreenToWorldPoint(new Vector3( Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y - hit.gameObject.transform.position.y)) - hit.gameObject.transform.position;
move.y = 0.0f;
hit.gameObject.rigidbody.MovePosition(hit.gameObject.rigidbody.position + move);
}
}
else if (Input.GetMouseButtonUp(0))
{
if(hit.gameObject.tag == "Enemy")
{
canMove = false;
hit.gameObject.rigidbody.useGravity = true;
hit.gameObject.rigidbody.freezeRotation = false;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
C# Randomly Adding Elements from stringListA to stringListB 1 Answer
C# Input.GetKey("Tab") Double Tap 1 Answer
C# Throw GameObject Upon Mouse Click 1 Answer
Player lives script help 1 Answer