- Home /
Collider/Rigidbody issue.Camera shaking!
So i have two cubes in my scene, one is the player who has a click to move script enabled and a rigidbody, and the other is the enemy who of course has a collider. The problem here is that i don't know how to make the player stop at a certain distance before the enemy so now he just crashes on the enemy and the whole camera shakes. Is there a solution for that?
Answer by Khada · Mar 12, 2013 at 03:29 PM
Sure, something like this should work:
Vector3 startPoint;
Vector3 endPoint;
float moveDist;
void Update()
{
//on mouse click
if(Input.GetMouseButtonDown(0))
{
endPoint = //whatever code you use to calculate the point to move to
//store the point where we are starting our new move
startPoint = transform.position;
//store the distance between the start point and end point
moveDist = (startPoint - endPoint).magnitude;
}
//if not at target move point
if(transform.position != endPoint)
{
//direction to move towards end point
Vector3 dir = (endPoint- transform.position).normalise;
//move towards end point
transform.position += dir * Time.delateTime;
//current distance from start point
float dist = (transform.position - startPoint).magnitude;
//if we have moved a distance greater than we should have
if(dist > moveDist)
transform.position = endPoint; //set us to target point
}
}
Hey thanks for that awesome answer. But where do i put the script?on the player right? And actually i get some errors that semicolons are missing :S Did you check that ClickTo$$anonymous$$ove script i use? http://wiki.unity3d.com/index.php/Click_To_$$anonymous$$ove_C
On the line:
endPoint = //whatever code you use to calculate the point to move to
you are supposed to replace the comment with whatever code you use to give the player a location to move to. Read through the comments and see if you can understand how it works, then work it into your project. It's up to you to work the solution into your specific code.
Have you gotten things working? Can you tick the answer if it's correct or post the answer yourself. This helps keep the site clean and give credit to those who take the time to help.
None of the 14 questions you've asked have had any answers be marked as correct. Please go through those and take a moment to give credit, where due, to the volunteers who give their time.
You are right. I forgot about this. But it says i have to log in as another user to Like :S
Answer by NikVel · Mar 27, 2016 at 01:54 PM
use FixedUpdate() in Camera script! It will fix problem/
No, it won't. Re-read the question to see why. And you've necro'd a 2 year old thread which is generally considered uncool.
Answer by programmrzinc · Mar 11, 2013 at 07:17 PM
The camera is shaking because the target of the camera is the Rigidbody Center of mass. Change the target to the Gameobject to fix this.
Are you sure it is going to work?any idea how to make the player sto in front of the enemy at a certain distance?
Can I see your script that controls the Player? I do not have a bearing on what is happening
It is not working unfortunately. I cannot change the target of the camera because the game i try to make is a dungeon cralwer action RPG (Diablo-style) and the camera needs to have the player as a target all the time. I use this script here to move my player. It's a ClickTo$$anonymous$$ove script...http://wiki.unity3d.com/index.php/Click_To_$$anonymous$$ove_C