- Home /
Detect Collision when moving an object
Hi everyone! I'm doing some tests in Unity and last day I made an obstacle that can be movable with the mouse wheel. But when the player collides with in, the obstacle can be movable anyway. Here's a video
And here is the script that I use for move the obstacle with mouse wheel:
#pragma strict
var targetAngle = 0;
var degreesPerClick = 2;
var secsPerClick = 0.3;
private var curAngle = 0f;
private var startAngle=0f;
private var startTime=0f;
function OnMouseOver () {
var clicks = Mathf.Round(Input.GetAxis("MouseWheel") * 100);
if (clicks != 0) {
targetAngle += clicks * degreesPerClick;
startAngle = curAngle;
startTime = Time.time;
}
var t = (Time.time - startTime) / secsPerClick;
if (t <= 1) {
curAngle = Mathf.Lerp(startAngle, targetAngle, t);
transform.eulerAngles.z = curAngle;
}
}
Thanks and sorry for my bad English :P
You're linked Youtube video is private, please make it public so others can view it. Only by reading your description, I'm unsure what your desired outcome is and what actually happens. Can you try to rephrase it more clearly?
Answer by theBlueBisu · May 06, 2015 at 01:39 PM
Hello
(Your video link is private)
I'm not sure of what want...
Some problems appear with scripted move and collisions. Your object has he a Rigidbody, on kinematic mode?
If the question is "why the scripted object move (turn if I understand well)", is just because you force it.
In this case, I think about:
Look towards check if a player(or object) is OnCollision. You can heard OnCollisionEnter/OnCollisionExit to modify a bool variable and test it before turn.
Use a combo object( Rigidbody kinematic,YourScript ) + object( Rigidbody, FixedJoin (or look others), Collider ) and with some adjustments make the join not rigid... (but I imagine an effect of elastic replacement which can appear, that you can maybe delete with taking rotation of the joined object if mousewheel==0, to not force rotation before the player has move)
Else to test if you can move or not you can test with RayCast, but in your case of rotation... not obvious
Thanks for your answer. I'll try it. Sorry but it's difficult to explain it. Now you can see the video, sorry but I didn't saw that it was in private mode.
Your answer
Follow this Question
Related Questions
Update () cant be a coroutine 2 Answers
How to create multiple choice question with audio clip 0 Answers
Adding a solid mesh 1 Answer
How can i insert a package? 1 Answer
Input key Conversion Query? 0 Answers