Object Follow Mouse Without Making The Collider Stops Working
So i have a cube and a sphere, i can move the cube using the mouse, but if i don't move the cube the ball hits the cube and changes the direction(thats what i want to happen), but if i move the cube and put it in front of the ball, the ball just passes trough the cube... Can someone help me??? (in everything im using vector 2 because its a 2d game, but im using 3d objects.
Code(For the cube): using UnityEngine; using System.Collections;
public class MouseDrag : MonoBehaviour {
void OnMouseDrag(){
Vector2 MousePosition = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
Vector2 objPosition = Camera.main.ScreenToWorldPoint (MousePosition);
transform.position = objPosition;
}
}
for the ball:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
float moveHorizontal = 1;
float moveVertical = 1;
public float speed;
int TouchRight = 0;
int touchDown = 0;
void FixedUpdate(){
if (TouchRight == 0 && touchDown == 0) {
Up ();
}
if(TouchRight == 1)
{
Right();
}
if(touchDown == 1)
{
Down();
}
}
void OnTriggerEnter(Collider other){
if (other.name == "ObstacleRight") {
TouchRight = 1;
}
if (other.name == "ObstacleDown") {
touchDown = 1;
}
}
void Up(){
Vector2 movement = new Vector2 (0f, moveVertical);
GetComponent<Rigidbody> ().velocity = movement * speed;
}
void Right(){
Vector2 movement = new Vector2 (moveHorizontal, 0f);
GetComponent<Rigidbody> ().velocity = movement * speed;
}
void Left(){
Vector2 movement = new Vector2 (moveHorizontal * (-1) , 0f);
GetComponent<Rigidbody> ().velocity = movement * speed;
}
void Down(){
Vector2 movement = new Vector2 (0f, moveVertical * (-1));
GetComponent<Rigidbody> ().velocity = movement * speed;
}
}
Answer by GingahNinja47 · Dec 24, 2018 at 02:55 PM
@Lukx31 I know this is a very late reply, but considering you don't have any I figured I might as well give you one. It's far from perfect, but this is the solution I used that works well enough:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speedMod;
public float farLeft, farRight, farTop, farBottom;
private Vector3 prevPos, currPos;
// Update is called once per frame
void FixedUpdate () {
if (Input.GetMouseButton(0))
{
if (Input.GetMouseButtonDown(0))
{
prevPos = Input.mousePosition;
}
currPos = Input.mousePosition;
}
Vector3 currWorldPos = currPos;
currWorldPos.z = 0;
Vector3 prevWorldPos = prevPos;
prevWorldPos.z = 0;
transform.position = new Vector3(
Mathf.Clamp(transform.position.x, farLeft, farRight),
Mathf.Clamp(transform.position.y, farBottom, farTop),
0);
Vector3 speed = (currWorldPos - prevWorldPos) * speedMod * Time.deltaTime;
GetComponent<Rigidbody>().velocity = speed;
prevPos = currPos;
}
}
Basically, rather than directly moving the object's position, you change the velocity on an attached Rigidbody. It takes some finagling with the speedMod to get it to line up just right, as different camera distances require different speeds to allow the object to keep up with the mouse, but it's the only way I've managed to get the colliders to work perfectly.
Your answer
![](https://koobas.hobune.stream/wayback/20220612051522im_/https://answers.unity.com/themes/thub/images/avi.jpg)