Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by NeaceTea · Feb 23, 2017 at 03:12 PM · c#collisioncharactercontrollercharacter.move

How in move make Y=1 everytime ?

(Sorry for my bad English i'm French)

Hello everybody, for the first time i'm posting here, i need your help :

I have a game who look like Click and Move, RTS, for now, i just have the character. Click Movement work, but when i place reflief on the terrain, the character start to move at Y=1 and go to Y=3 and fly between original position and the mountain. If it's possible, how can i make my character go to Y=3 without flying ? Doing Y=1 all time, and when encounter a mountain, up to this mountain ? Same thing when the character start moving from y=3 to y=1.

Here is my code :

 public class ClickMove : MonoBehaviour {
     Ray ray;
     RaycastHit hit;
     public float speed;
     Vector3 newpos;
     void Start()
     {
         newpos = transform.position;
         hit.point = transform.position;
     }
     void Update()
     {
         float step = speed * Time.deltaTime;
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Input.GetKeyDown(KeyCode.Mouse1))
         {
             if (Physics.Raycast(ray, out hit, 100))
             {
                 newpos = new Vector3(hit.point.x, hit.point.y, hit.point.z);
                 newpos.y = newpos.y + 1f;
                 Debug.Log(hit.point);
             }
         }
         Vector3 direction = newpos - transform.position;
         Vector3 movement = direction.normalized * step;
         if (movement.magnitude > direction.magnitude)
         {
             movement = direction;
         }
 
         GetComponent<CharacterController>().Move(movement);
            // transform.position = Vector3.MoveTowards(transform.position, newpos, step);    //LAST MOVEMENT BEFORE MODIFICATION
     }
 }

Here is a picture (With paint :D) In green what i want, in red what i have .

alt text

Thanks for your answers and again, sorry for my bad English.

whatiwant.png (6.2 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityCoach · Feb 23, 2017 at 08:47 PM

Hi,

I'm not totally sure about goes wrong, but I figured I start with a few optimisation recommendation, and you can tell me more about what you want to do.

1st, you don't want to call GetComponent every frame as it takes performances down. Instead, you want to make a reference to the component, like :

 public class ClickMove : MonoBehaviour
 {
      Ray ray;
      RaycastHit hit;
      public float speed;
      Vector3 newpos;
 
     private CharacterController _charController; // component reference
      private void Awake ()
      {
         _charController = GetComponent<CharacterController>(); // reference assignment
      }
 
      void Start()
      {
          newpos = transform.position;
          hit.point = transform.position;
      }
      void Update()
      {
          float step = speed * Time.deltaTime;
          ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          if (Input.GetKeyDown(KeyCode.Mouse1))
          {
              if (Physics.Raycast(ray, out hit, 100))
              {
                  newpos = hit.point; // you can simply pass the point info, it already is a Vector3
                  newpos.y = newpos.y + 1f;
                  Debug.Log(hit.point);
              }
          }
          Vector3 direction = newpos - transform.position;
          Vector3 movement = direction.normalized * step;
          if (movement.magnitude > direction.magnitude)
          {
              movement = direction;
          }
  
          _charController.Move(movement); // use of preassigned component
             // transform.position = Vector3.MoveTowards(transform.position, newpos, step);    //LAST MOVEMENT BEFORE MODIFICATION
      }
  }

Then, what I believe is missing, is a terrain path sampler. Like, from A to B, you're going over different altitudes, and I don't see anything like that in your code. Which makes me think I may have misunderstood what you want. Let me know. ps : I speak French too ;)

Comment
Add comment · 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

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

331 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 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 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

Unity physics Help Implementing Real Life Physics in Jumping System C# 2 Answers

How to make 2d character stop moving after collision with a box collider? 0 Answers

How to manage healthbar with collision?,How to manage health with collision 0 Answers

How to deadtivate a gameobjects from the scene, if player has a character controller 0 Answers

How do I use the capsule Collider? 1 Answer


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