- Home /
My code break character controller
Hi guys, I am right now stuck in a problem. The code is below.
When someone click in the button b1 he will move to the position that i set. It is working but my main camera with the character controller; motor; fpsinput; stop to move. When i deactivate my code, the main camera return to move. If someone could explain where is the fault i appreciate.
using UnityEngine;
using System.Collections;
public class DetectObject : MonoBehaviour {
public Vector3 destino;
public Vector3 origem;
public bool mover = false;
// Use this for initialization
void Start () {
origem = Camera.main.transform.position;
destino = origem;
}
// Update is called once per frame
void Update () {
RaycastHit hit;
Ray ray;
if (mover) {
Camera.main.transform.position = Vector3.Lerp(origem, destino, 0.1f);
origem = Camera.main.transform.position;
}
if (Vector3.Distance(origem,destino)<0.01f) {
Camera.main.transform.position = destino;
mover = false;
}
if (Input.GetButtonDown("Fire1")) {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
if (hit.collider.gameObject.GetInstanceID()==GameObject.Find("b1").GetInstanceID()){
Debug.Log("b1");
destino = new Vector3(48,-33,1234);
origem = Camera.main.transform.position;
mover = true;
}
}
}
}
}}
Answer by Bunny83 · Oct 12, 2011 at 12:45 AM
Your problem is that you set the Cameras position each frame, even when you don't move. That's because you have reached your target and therefore the distance keeps below 0.01f.
Just do the distance check within the if (mover)
if (mover)
{
Camera.main.transform.position = Vector3.Lerp(origem, destino, 0.1f);
origem = Camera.main.transform.position;
if (Vector3.Distance(origem,destino)<0.01f)
{
Camera.main.transform.position = destino;
mover = false;
}
}
Another thing which looks really strange is:
hit.collider.gameObject.GetInstanceID()==GameObject.Find("b1").GetInstanceID()
I guess that almost the slowest and complicated way of checking the hitted object whether it's the "b1" gameobject or not.
GameObject.Find is very very slow and it's not necessary. GameObject.Find will search for a GameObject which name is "b1". If you want to know if the gameobject you've hit is that gameobject, just check the name:
if (hit.collider.name == "b1")
All components (including the Collider) share the same name with the GameObject they are attached to.
Answer by aldonaletto · Oct 12, 2011 at 01:04 AM
Maybe your problem is caused by the camera never reaching the minimum distance to stop and change mover to false - you can set the minimum distance to 0.1f, for instance, to check this.
But you have another problem: in the First Person Controller, the camera is a child of the player; if you move it, you change its local position. When stopping, the camera stays at this position relative to the player - when you move the player, the camera follows it, but keeping this relative position, what is very weird.
Anyway, there's an easier way to do the same thing: attach the script to the object "b1" and use OnMouseDown to detect when the object is clicked, then use MoveTowards to move the camera to the destination. You can also create an empty object at the destination position and use it directly, without having to write the coordinates by hand - is much easier, and if you want to change the destination, just move the empty object to the new end point:
public Transform objetoDestino; // drag the destination empty object here public float speed = 4.0f; // speed to move (m/s) bool mover = false; Vector3 destino;
void OnMouseDown() {
mover = true;
destino = objetoDestino.position;
}
void Update () {
if (mover){
Camera.main.transform.position = Vector3.MoveTowards(
Camera.main.transform.position,
destino,
speed*Time.deltaTime
);
if (Vector3.Distance(Camera.main.transform.position, destino)<0.01f){
mover = false;
}
}
}
Vector3.MoveTowards(origem, destino, dist) returns a point in the line formed by origem and destino at a distance dist from the origem. dist is clamped internally to never pass the destino point, no matter how big dist is
With two response i am sure this topic is clear.
Thanks too.
Your answer
![](https://koobas.hobune.stream/wayback/20220613004558im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to lerp over time help? 1 Answer
using yield in components 2 Answers
Sprite Color.Lerp 0 Answers
Lerp movement speed 1 Answer
This segment of code is causing problems 2 Answers