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 Arpit8081 · Sep 30, 2020 at 02:44 PM · charactercontrollertimemoving

Character moving

Hi,

I have 2 characters and one car. I want to do when the car reaches a certain z position like -0.02 or 0.02. the characters need to start walking. Before that character will be stopped at that place. here is my full code

``` using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Experimental.VFX;

public class CharaterNavigationCntroller : MonoBehaviour { public float movementSpeed = 0.5f; public float rotationSpeed = 120f; public float stopDistance = 1f; public Vector3 destination; public Animator animator; public bool reachedDestination;

 private Vector3 lastPosition;
 Vector3 velocity;

 public Transform car;
 //0.34936   last point: 0.4864168
 private void Awake()
 {
     movementSpeed = Random.Range(0.1f,0.2f);
     animator = GetComponent<Animator>();
 }
 void Update()
 {
     if (car.transform.position.z >= 0.34936f)
     {
         animator.SetBool("IsWalking", true);
         if (transform.position != destination)
         {
             Vector3 destinationDirection = destination - transform.position;
             destinationDirection.y = 0;

             float destinationDistance = destinationDirection.magnitude;

             if (destinationDistance >= stopDistance)
             {
                 reachedDestination = false;
                 Quaternion targetRoation = Quaternion.LookRotation(destinationDirection);
                 transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRoation, rotationSpeed * Time.deltaTime);
                 transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
             }
             else
             {
                 reachedDestination = true;
             }

             velocity = (transform.position - lastPosition) / Time.deltaTime;
             velocity.y = 0;
             var velocityMagnitude = velocity.magnitude;
             velocity = velocity.normalized;
             var fwdDotProduct = Vector3.Dot(transform.forward, velocity);
             var rightDotProduct = Vector3.Dot(transform.right, velocity);

             animator.SetFloat("Hornizontal", rightDotProduct);
             animator.SetFloat("Forward", fwdDotProduct);
         }

         //lastPosition = transform.position;
         else
         {
             reachedDestination = true;
         }
     }
     else
     {
         animator.SetBool("IsWalking", false);
     }
 }
 public void SetDestination(Vector3 destination)
 {
     this.destination = destination;
     reachedDestination = false;
 }

}

``` Does anyone have an idea how can I approach this idea in unity?

"All characters are made in Fuse and mixamo software".

Comment
Add comment · Show 7
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 Klarzahs · Oct 01, 2020 at 10:59 AM 0
Share

There are two approaches to it: You can either use colliders and triggers to detect if the car is at the position, or simply query the cars position in each persons update function

avatar image Arpit8081 Klarzahs · Oct 01, 2020 at 12:11 PM 0
Share

Please see below comment.

avatar image Arpit8081 · Oct 01, 2020 at 12:10 PM 0
Share

I try with this script.

```if (car.transform.position.z == 0.34936 || car.transform.position.z >= 0.4864168) { animator.SetBool("IsWalking", true); } else { animator.SetBool("IsWalking", false); } ``` But When I reach to that position the character is not walking. Also, I try with the in first position then the character is only walking. he does not stop at that place.

avatar image Klarzahs Arpit8081 · Oct 01, 2020 at 01:36 PM 0
Share

Did you debug it, aka does the "if" actually trigger? As a sidenote: its never good to check whether a float is exactly a value (z == 0.34936), as they very rarely are truly the same value. $$anonymous$$aybe the if is not actually triggered that way

avatar image Arpit8081 Klarzahs · Oct 01, 2020 at 01:46 PM 0
Share

But if I try it's working but it's not stopped even if the car is not that position.

Show more comments
avatar image Arpit8081 Arpit8081 · Oct 01, 2020 at 02:04 PM 0
Share

https://www.youtube.com/watch?v=HgW-rAxnEqc&ab_channel=CBShowtunes

I am using this script 23.10

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by haliler921 · Oct 01, 2020 at 12:49 PM

After placing the cube at a point you want the car to reach, close the mesh renderer of the cube. Don't forget to add colliders to your car and your cube! To detect the collision inside the cube (car vs cube collision) write the required codes Note: If your car is a solid substance, ie your player dies when it hits the player, the code you need to write to detect the collision is the one with OnCollisionEnter. But if it's not a solid substance then your car is the OnTriggerEnter code. After checking if there is a collision, the code you need to write inside your players has this collision happened? if move on, if not, keep stopping

Comment
Add comment · Show 4 · 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 Arpit8081 · Oct 01, 2020 at 01:02 PM 0
Share

If you see the comment code. you will get an idea. Before I press the start button the car is in that place then the player starts to walk otherwise character in Idle position.

avatar image Arpit8081 · Oct 01, 2020 at 01:05 PM 0
Share

Once I start the game and my car will reach that position the character is still walking.

avatar image haliler921 Arpit8081 · Oct 02, 2020 at 12:05 PM 0
Share

Line 40 Line 40 animator.SetFloat ("Hornizontal", rightDotProduct); shouldn't be

animator.SetFloat ("Horizontal", rightDotProduct); must be

avatar image Arpit8081 haliler921 · Oct 02, 2020 at 02:11 PM 0
Share

Then what it should be?

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

159 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Another question about Skyroads-like moving 0 Answers

Timed actions 1 Answer

Moving objects when timescale is high, which .time to use ? 2 Answers

CharacterController - moving while jumping (movement vector values reset?) 1 Answer

if (controller.isGrounded == false) (for more than) public int time = 1.0F; // do stuff 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