- Home /
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!
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
}
}
}
I compiled this with a simple scene for webplayer if you want to test it http://dl.dropbox.com/u/10242742/WebPlayer/WebPlayer.html
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
Your answer
Follow this Question
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