Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nowhere · Oct 11, 2011 at 11:33 PM · lerp

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;
                 
             }
             
             
             }
             
         }
     }
     
 }}
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nowhere · Oct 12, 2011 at 05:28 AM 0
Share

Thanks to the complete explanation and for the tip.

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nowhere · Oct 12, 2011 at 05:28 AM 0
Share

With two response i am sure this topic is clear.

Thanks too.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges