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 jw120691 · Oct 13, 2011 at 04:10 PM · movementtransformuntiy

Selecting and moving an individual object during gameplay..

Hello! I am very new to Unity (currently making my first game) and I am trying to write a script to select and move an individual cuboid object forwards and backwards using the up and down arrow keys. There are 5 cuboid objects in my game and at the moment I have successfully written a script to move them all at the same time but I want to be able to individually move them after selecting them with the mouse during gameplay.

The script I currently have is:

function Update () {

 var speed = 5.0;

 var move : float = Input.GetAxis("Vertical");
 
 Debug.Log(move);
 
 transform.Translate(Vector3(0, 0, move) * Time.deltaTime * speed);

}

Thanks!

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 chemicalvamp · Oct 13, 2011 at 06:47 PM

This is like double overkill, but i couldnt think of a simple way to explain it to you.. so i started a project and got carried away :) take this script and attach it to your camera, and drag a primitive object prefab onto the script, sorry it is in C# but i commented it as much as i could. (some explanations may be wrong, im new to unity)

 using UnityEngine;
 
 public class BoxMove : MonoBehaviour
 {
 
     public GameObject primitivePrefab; // the prefab object you will create and control must have a rigidbody component!
     public float speed = 10f; // the speed w a s d will move the rigidbody
     public float gravity = 10f; // the speed the rigid body will be pulled down
     public float maxVelocityChange = 10; // for clamping rigidbodys new velocity
 
     private GameObject currentlyControlled; // the rigidbody w a s d will move
     private bool isFire1Held = false; // without these two Fire1 and Fire2 actions would be done every frame
     private bool isFire2Held = false;
     private RaycastHit Fire1hitinfo; // these store the collision data of the latest raycast
     private RaycastHit Fire2hitinfo;
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1") && !isFire1Held)
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // casts a ray from the center of the camer to the mouse pointer
             if (Physics.Raycast(ray, out Fire1hitinfo))
                 Instantiate(primitivePrefab, Fire1hitinfo.point, transform.rotation); // create the object where the ray collided
             isFire1Held = true;
         }
         else if (Input.GetButtonUp("Fire1"))
         {
             isFire1Held = false;
         }
 
         if (Input.GetButtonDown("Fire2") && !isFire2Held)
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // casts a ray from the center of the camer to the mouse pointer
             if (Physics.Raycast(ray, out Fire2hitinfo))
             {
                 currentlyControlled = Fire2hitinfo.collider.gameObject; // set the currently controlled rigid body to the object the raycast hit
                 currentlyControlled.transform.rotation = new Quaternion(0, 0, 0, 0); // adjust the rigidbodys rotation so w a s d will properly control it
                 currentlyControlled.rigidbody.constraints = RigidbodyConstraints.FreezeRotation; // freeze the rotation so moving it will not mess up proper control
             }
 
             isFire2Held = true;
         }
         else if (Input.GetButtonUp("Fire2"))
         {
             isFire2Held = false;
         }
     }
 
     void FixedUpdate()
     {
         if (currentlyControlled != null)
         {
             Vector3 targetVelocity = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); // this vector is made from axis(s) held
             targetVelocity = currentlyControlled.transform.TransformDirection(targetVelocity);
             targetVelocity *= speed; // multiple our target velocity by speed (as it is only 0 1 or -1 right now)
 
             Vector3 velocity = currentlyControlled.rigidbody.velocity; // add our velocity to controlled rigidbody
             Vector3 velocityChange = (targetVelocity - velocity); // this is for acceleration
             velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
             velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
             velocityChange.y = 0;
             currentlyControlled.rigidbody.AddForce(velocityChange, ForceMode.VelocityChange); // move the rigidbody in game space
         }
     }
 }
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 chemicalvamp · Oct 14, 2011 at 07:18 AM 0
Share

I compiled this with a simple scene for webplayer if you want to test it http://dl.dropbox.com/u/10242742/WebPlayer/WebPlayer.html

avatar image
0

Answer by satolas · Feb 12, 2018 at 06:23 AM

Hi !

This is a very very old post, but it is exactely what I want to do for a project. So I tried it but it seems that is not working anymore...
The way the rigid body was retrieved was giving me an error, so I updated the code to a newer syntax :No error anymore...but

1) In runtime when I click: The script do instantiate the prefab but after that I'm not able to move it.

2) Also I would like to do a variant where you would actually able to move objects that already exist in the scene by clicking on them instead of instantiating new ones.
If the post is too old to continue it, I'll do a new topic, but since this one exactely match, could be interesting to update it.

             using System.Collections;
             using System.Collections.Generic;
             using UnityEngine;

             public class BoxMove : MonoBehaviour
             {
                 //https://answers.unity.com/questions/176298/selecting-and-moving-an-individual-object-during-g.html

                 public GameObject primitivePrefab; // the prefab object you will create and control must have a rigidbody component!
                 public float speed = 10f; // the speed w a s d will move the rigidbody
                 public float gravity = 10f; // the speed the rigid body will be pulled down
                 public float maxVelocityChange = 10; // for clamping rigidbodys new velocity

                 private GameObject currentlyControlled; // the rigidbody w a s d will move
                 private bool isFire1Held = false; // without these two Fire1 and Fire2 actions would be done every frame
                 private bool isFire2Held = false;
                 private RaycastHit Fire1hitinfo; // these store the collision data of the latest raycast
                 private RaycastHit Fire2hitinfo;

                 public Rigidbody rb;

                 private void Start()
                 {
                     rb = primitivePrefab.GetComponent<Rigidbody>();
                 }

                 void Update()
                 {
                     if (Input.GetButtonDown("Fire1") && !isFire1Held)
                     {
                         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // casts a ray from the center of the camer to the mouse pointer
                         if (Physics.Raycast(ray, out Fire1hitinfo))
                             Instantiate(primitivePrefab, Fire1hitinfo.point, transform.rotation); // create the object where the ray collided
                         isFire1Held = true;
                     }
                     else if (Input.GetButtonUp("Fire1"))
                     {
                         isFire1Held = false;
                     }

                     if (Input.GetButtonDown("Fire2") && !isFire2Held)
                     {
                         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // casts a ray from the center of the camer to the mouse pointer
                         if (Physics.Raycast(ray, out Fire2hitinfo))
                         {
                             currentlyControlled = Fire2hitinfo.collider.gameObject; // set the currently controlled rigid body to the object the raycast hit
                             currentlyControlled.transform.rotation = new Quaternion(0, 0, 0, 0); // adjust the rigidbodys rotation so w a s d will properly control it
                             //currentlyControlled.rigidbody.constraints = RigidbodyConstraints.FreezeRotation; // freeze the rotation so moving it will not mess up proper control
                             rb.constraints = RigidbodyConstraints.FreezeRotation;
                         }

                         isFire2Held = true;
                     }
                     else if (Input.GetButtonUp("Fire2"))
                     {
                         isFire2Held = false;
                     }
                 }

                 void FixedUpdate()
                 {
                     if (currentlyControlled != null)
                     {
                         Vector3 targetVelocity = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); // this vector is made from axis(s) held
                         targetVelocity = currentlyControlled.transform.TransformDirection(targetVelocity);
                         targetVelocity *= speed; // multiple our target velocity by speed (as it is only 0 1 or -1 right now)

                         //Vector3 velocity = currentlyControlled.rigidbody.velocity; // add our velocity to controlled rigidbody
                         Vector3 velocity = rb.velocity;
                         Vector3 velocityChange = (targetVelocity - velocity); // this is for acceleration
                         velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
                         velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
                         velocityChange.y = 0;
                         //currentlyControlled.rigidbody.AddForce(velocityChange, ForceMode.VelocityChange); // move the rigidbody in game space
                         rb.AddForce(velocityChange, ForceMode.VelocityChange);
                     }
                 }
             }


Cheers

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Selecting and moving an individual object during gameplay.. 2 Answers

Updating rotation of client not working - strange error 0 Answers

Network Trasnform Interpolation Factor 2 Answers

Finding local position and clamping movement 2 Answers

Can you pass along something like transform.position.x or y? not the value but the VARIABLE? 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