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 dna1978 · Aug 28, 2015 at 06:55 PM · input

PointAndClick

How can i make the third person character from the sample assets walk via point and click.I added some lines of code to the controller into the FixedUpdate Method. ...

  if (Input.GetMouseButton (0)) {
   RaycastHit hit;
   Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    if (Physics.Raycast (ray, out hit, 1000)) {
     move = new Vector3 (hit.point.x, hit.point.y, hit.point.z);
  }
 }

... But it is not working. Can someone help? Thanks

Comment
Add comment · Show 1
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 hexagonius · Aug 28, 2015 at 08:17 PM 0
Share

hit.point is already a Vector3, you can use it. Is the code reaching the move assignment (does the raycast work? set a debug to check)? are you doing anything with the move variable afterwards?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dna1978 · Aug 31, 2015 at 01:35 PM

I pasted the complete controller code.As i said it is from the sample assets.The lines i added do not work,so i commented them out.WASD movement works.Point and click movement should be easy to add,but i am to unexperienced to get it working. Thanks for help.

using UnityEngine; using UnitySampleAssets.CrossPlatformInput;

namespace UnitySampleAssets.Characters.ThirdPerson {

 [RequireComponent(typeof (ThirdPersonCharacter))]
 public class ThirdPersonUserControl : MonoBehaviour
 {

     public bool walkByDefault = false; // toggle for walking state

     public bool lookInCameraDirection = true;// should the character be looking in the same direction that the camera is facing

     private Vector3 lookPos; // The position that the character should be looking towards
     private ThirdPersonCharacter character; // A reference to the ThirdPersonCharacter on the object
     private Transform cam; // A reference to the main camera in the scenes transform
     private Vector3 camForward; // The current forward direction of the camera

     private Vector3 move;
     private bool jump;// the world-relative desired move direction, calculated from the camForward and user input.

     // Use this for initialization
     private void Start()
     {
         // get the transform of the main camera
         if (Camera.main != null)
         {
             cam = Camera.main.transform;
         }
         else
         {
             Debug.LogWarning(
                 "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.");
             // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
         }

         // get the third person character ( this should never be null due to require component )
         character = GetComponent<ThirdPersonCharacter>();
     }

     void Update()
     {
         /*if(!jump)
             jump = CrossPlatformInputManager.GetButtonDown("Jump");*/
     }

     // Fixed update is called in sync with physics
     private void FixedUpdate()
     {
         // read inputs
         bool crouch = false;

         float h = CrossPlatformInputManager.GetAxis("Horizontal");
         float v = CrossPlatformInputManager.GetAxis("Vertical");
         crouch = Input.GetKey(KeyCode.C);

         /*if (Input.GetMouseButton (0)) {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             if (Physics.Raycast (ray, out hit, 1000)) {
                 move = new Vector3 (hit.point.x, hit.point.y, hit.point.z);
             }
         }*/

         // calculate move direction to pass to character
         if (cam != null)
         {
             // calculate camera relative direction to move:
             camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
             move = v*camForward + h*cam.right;
         }
         else
         {
             // we use world-relative directions in the case of no main camera
             move = v*Vector3.forward + h*Vector3.right;
         }

         if (move.magnitude > 1) move.Normalize();

if !MOBILE_INPUT

        // On non-mobile builds, walk/run speed is modified by a key press.
         bool walkToggle = Input.GetKey(KeyCode.LeftShift);
         // We select appropriate speed based on whether we're walking by default, and whether the walk/run toggle button is pressed:
         float walkMultiplier = (walkByDefault ? walkToggle ? 1 : 0.5f : walkToggle ? 0.5f : 1);
         move *= walkMultiplier;

endif

         // calculate the head look target position
         lookPos = lookInCameraDirection && cam != null
                       ? transform.position + cam.forward*100
                       : transform.position + transform.forward*100;

         // pass all parameters to the character control script
         character.Move(move, crouch, jump, lookPos);
         jump = false;
     }
 }

}

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

27 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

Related Questions

Gameobject moving too far on Input 1 Answer

How do I change the size in Input Manager? 1 Answer

Efficient way to use UGui.Overlap for a GUI Mouse 0 Answers

input dosnt work while idle animation is playing 1 Answer

Player follow touch when holding down your finger on the screen (C#) 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