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 Windsofthepast · Oct 05, 2016 at 10:25 PM · scripting problemscripting beginnerplayer movementridgidbody

Creating Objects on Right Click to Navigate Around

A class assignment that I am working on requires me to have a player character move with left click and create objects the player character cannot pass through with right click.

I am having issues with my player character interacting with impassible objects, however, where the game eventually thinks that the player has reached the destination, even if it hasn't. Furthermore, the player object does not navigate around the object, and I am unable to figure out how to get it to do so. Also, for some reason, when I attempt to create a new impassible object, the game is creating two and placing one at the point I clicked, and the other on the middle of the plane.

This is the code that I have:

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript : MonoBehaviour
 {
     // Max Speed Variable
     private float MaxSpeed;
 
     // Flag for If Clicked
     private bool ifClicked;
 
     // Player y axis location
     private float playeryAxis;
 
     // Destination Point
     private Vector3 endPoint;
 
     // Create Object Prefab
     
 
     // Location Points for Righ Click Objects
     private Vector3 objPoint;
 
 
     // Use this for initialization
     void Start ()
     {
         // Set Max Speed
         MaxSpeed = 10f;
 
         // Set ifClicked Flag to False
         ifClicked = false;
 
         // Get the y Axis value of the Player
         playeryAxis = gameObject.transform.position.y;
     }
     
     // Update is called once per frame
     void Update ()
     {
         if(Input.GetMouseButtonDown(0) && !ifClicked)
         {
             RaycastHit hit;
             // Ray for Clicked Position
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             // Check if Ray hits Collider
 
             if (Physics.Raycast(ray,out hit))
             {
                 // Set ifClicked Flag to true
                 ifClicked = true;
                 endPoint = hit.point;
                 // Make sure Player does not move to incorrect y Axis point
                 // Only x Axis is necessary
                 // Return y Axis to original point
                 endPoint.y = playeryAxis;
             }
         }
 
         // Check if ifClicked is true and if Player is not at clicked location
         if (ifClicked && !(gameObject.transform.position.magnitude == endPoint.magnitude))
         {
             gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1 / (MaxSpeed * (Vector3.Distance(gameObject.transform.position, endPoint))));
         }
 
         // Else if ifClicked is true and Player is at clicked location set ifClicked back to false
         else if (ifClicked && (gameObject.transform.position.magnitude == endPoint.magnitude))
         {
             ifClicked = false;
         }
 
         // If Right Clicked
         if (Input.GetMouseButtonDown(1))
         {
             RaycastHit hit;
             // Ray for Clicked Position
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             // Check if Ray hits Collider
             if (Physics.Raycast(ray, out hit))
             {
                 // Get position for Object
                 objPoint = hit.point;
                 // Make sure Object is on the same y Axis as Player
                 objPoint.y = playeryAxis;
                 Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), new Vector3(objPoint.x, objPoint.y, objPoint.z), Quaternion.identity);
                 
             }
         }
     }
 }
 

I am still new to unity and C#, so I'm not really sure what all I'm doing. Any help would be greatly appreciated.

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

0 Replies

· Add your reply
  • Sort: 

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

90 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

Related Questions

Game Object 2 different movement pivots 0 Answers

How to change scene when video ends? 0 Answers

FirstPersonController AudioClip component interrupt Background audio from the BackgroundAudio AudioClip 0 Answers

Unable to pass values from one script to another 1 Answer

Disable 2 Scripts during Countdown 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