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 thumper_the_great · Dec 21, 2015 at 05:26 PM · vector3transform.positionplayer movementraycasthit

Using Raycast hit to set vector position then moving player to that position

Hi All,

I know there are many topics on this area however I am yet to find a solution. I'm new to unity, am grasp the logic behind the code OK however am struggling with trying to use Raycast Hit for a none flat terrain.

my scene link

Tutorial I'm Following

I've made some small changes to the namimg conventions but apart from that everything is exact from what I can tell.

The SimpleMove function doesn't appear to work. No errors are received when running the script. All the player does is rotate that it. Doen't move from it's position. Have tried using other methods (one is commented out in the below code) but to no avail. Would really like to learn more as I progress through this tutorials. Help very much appreciated.

This method works but I want to use the Raycast hit to detect if it's an enemy or an item I have clicked on.

Have tried playing around with navmesh agents instead where character can move around but can't get the navmesh and raycast hit working. Any help again appreciated.

The code I have below.

 void Awake()
 {
     //anim = GetComponent<Animator>();
 }

 void Start()
 {
     targetPosition = transform.position; // will set the target position to our current position on start
 }

 // Update is called once per frame

 void Update()
 {
     //if the player clicked on the screen, find out where
     if (Input.GetMouseButton(LEFT_MOUSE_BUTTON))
     {
         SetTargetPosition();
     }
     MovePlayer();
 }

 void SetTargetPosition() // set position we will travel too
 {
     RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

     if (Physics.Raycast(ray, out hit, 1000))
     {
         targetPosition = new Vector3(hit.point.x, hit.point.y, hit.point.z);

     }
 }

 void MovePlayer()
 {
     if (Vector3.Distance(transform.position, targetPosition) > 1)
     {
         Quaternion newRotation = Quaternion.LookRotation(targetPosition - transform.position, Vector3.forward);

         newRotation.x = 0f;
         newRotation.z = 0f;

         transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
         controller.SimpleMove(transform.forward * walkspeed);
         //transform.position = Vector3.MoveTowards(transform.position, targetPosition, walkspeed * Time.deltaTime);
         Debug.DrawLine(transform.position, targetPosition, Color.red);
     }
 }

}

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

Answer by wibble82 · Dec 21, 2015 at 05:36 PM

Hi

I'm not sure if it could be causing the issue, but to start with your quaternion code is definitely bad! :)

You can not simply set the x/z values of a quaternion. Perhaps you're confusing it with the euler angles (displayed in the inspector)? I suspect what you're trying to do is point the rotation at the target in terms of y rotation, but leave x and z rotation alone? For this, the simplest approach would be to just make your 'target' be at the same z as your transform:

            void MovePlayer()
             {
                 Vector3 currentTarget = targetPosition;
                 currentTarget.y = transform.position;
 
                 if (Vector3.Distance(transform.position, currentTarget) > 1)
                 {
                     Quaternion newRotation = Quaternion.LookRotation(currentTarget - transform.position, Vector3.forward);
 
                     transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
 
                     controller.SimpleMove(transform.forward * walkspeed);
                     //transform.position = Vector3.MoveTowards(transform.position, targetPosition, walkspeed * Time.deltaTime);
 
                     Debug.DrawLine(transform.position, currentTarget, Color.red);
                 }
             }

I'm not sure exactly what the controller.SimpleMove does - presumably you have another script that the controller is referencing, so that could be screwing with your transform. Without knowing more about it I couldn't say though.

All that said, just to be sure, have you made 100% certain that walkspeed is not 0? (just checking!)

-Chris

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 thumper_the_great · Dec 22, 2015 at 08:46 AM 0
Share

Thanks. for the suggestion. I tried that but had no luck.

I assume the Simple$$anonymous$$ove is a unity function as the tutorial doesn't point out any other scripts. Only adding the controller as a component to the player.

Have been looking for more information and have decided to go with the nav$$anonymous$$eshAgent to for pathfinding which rules out the problems for now ( will look into disabling nav$$anonymous$$eshAgent rotation (for the player) and speed. I want to set the rotation for the player as the nav$$anonymous$$esh rotation doesn't look good and I eventually want to toggle run or walk.

Now I'm trying to detect if my ray hits another object which is tagged.

 void SetTargetPosition() // set position we will travel too
 {
     RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     Plane plane = new Plane(Vector3.up, transform.position);
     float point = 0f;
     

     if (Physics.Raycast(ray, out hit))
     {
         Debug.Log("Is this working?");

         if (hit.collider.tag == "Enemy")
         {
             Debug.Log("we are going to attack!");
         }
         else if (hit.collider.tag == "Item")
         {
             Debug.Log("Lets go pick up whatever this is");
         }

         else if (hit.collider.tag == "Wall")
         {
             Debug.Log("im going to run into a wall");
             Debug.DrawLine(transform.position, targetPosition, Color.red);
         }

         else if (plane.Raycast(ray, out point))
         {
             targetPosition = ray.GetPoint(point);
             Debug.Log("we are going for a casual stroll!");
             Debug.DrawLine(transform.position, targetPosition, Color.green);
         }
     }
 }


 void $$anonymous$$ovePlayer()
 {
     //Quaternion targetRotation = Quaternion.LookRotation(targetPosition - transform.position);
     //transform.rotation = targetRotation;
     //transform.LookAt(targetPosition);
     //transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPosition, walkspeed * Time.deltaTime);
     agent.SetDestination(targetPosition);
 
 
 }

}

It can distinguish between where it can walk (casual stroll) and if it will hit a wall using the debug draw line red and green. Having problems with overlapping navmesh (have a 2nd floor with stairs) and looking into that as well.

I'm trying to get the raycast to detect an enemy but i think that problem has more to do with the enemy mesh collider rather than the code functionality.

If you could suggest anything else that I can try on the above point I'll appreciate it otherwise i'll mark this as complete.

avatar image
0

Answer by thumper_the_great · Dec 22, 2015 at 02:59 PM

Answer in my comment. Have used navMeshAgent instead for pathfinding wilst still be able to use a Raycast Hit to determine what object i've hit with the raycast

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

35 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

Related Questions

Move GameObject to Point A to B 1 Answer

instantiate keeps repeating itself 0 Answers

Main Camera automatically increment by 0.75 on Y axis 0 Answers

Vector3 is not receiving enough arguments from a another script that gives out transform.position. 1 Answer

Falling object respawner 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