Jumping with non-rigid character.,Dragged object Enters Ground freely
I try to look up tutorials for my problem, but they all deal with rigidbody instead of controller and colliders. How can i make my character jump?,I'm creating a game and i want to prevent the object the player grabs from entering the ground. I also want to prevent the player from flying if the object is put below them. Please Help.
Here is the drag/drop code,
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DragObject : MonoBehaviour {
//Initialize Variables
GameObject getTarget;
bool isMouseDragging;
Vector3 offsetValue;
Vector3 positionOfScreen;
// Use this for initialization
void Start()
{
}
void Update()
{
//Mouse Button Press Down
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo;
getTarget = ReturnClickedObject(out hitInfo);
if (getTarget != null)
{
isMouseDragging = true;
//Converting world position to screen position.
positionOfScreen = Camera.main.WorldToScreenPoint(getTarget.transform.position);
offsetValue = getTarget.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, positionOfScreen.z));
}
}
//Mouse Button Up
if (Input.GetMouseButtonUp(0))
{
isMouseDragging = false;
}
//Is mouse Moving
if (isMouseDragging)
{
//tracking mouse position.
Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, positionOfScreen.z);
//converting screen position to world position with offset changes.
Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offsetValue;
//It will update target gameobject's current postion.
getTarget.transform.position = currentPosition;
}
}
//Method to Return Clicked Object
GameObject ReturnClickedObject(out RaycastHit hit)
{
GameObject target = null;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction * 10, out hit))
{
target = hit.collider.gameObject;
}
return target;
}
}
Your answer
Follow this Question
Related Questions
Collisions won't work(Please check text) 1 Answer
Any way to add physical box colliders to an object with Character Controller ? 0 Answers
character feet go slightly beneath floor in certain situations 0 Answers
Proper use of Character Controller 2 Answers
Controller not colliding with ANYTHING [PLZ HELP ASAP] 0 Answers