- Home /
Placing an object around character in 3d space?
Hi!
Im trying to make a shield that can be placed around the player, in the player desired position, in the 3d world, in order to block some shots...
I was thinking on making a prefb, and instantiating it on the desired position, with some sort of orbit. Any Idea on how to make this? Here an ilustration.
I apreciate it. :D
Answer by robertbu · Apr 24, 2014 at 06:05 AM
Why not just have a Quad as a child of the player. You can turn the renderer and the collider on and off to turn the shield on and off. You can move the shield to desired position as needed. No reason to instantiate a game object. You don't give any details on the movement code, so I cannot be more specific on how to position the Quad.
Hi Robertu, Thank you for taking some time.
I'm quite new at this, I dident post the code because i dident thought it was relevant *silly me.
I was trying to undertstand what quad was but I think it is kind of difficult (been progra$$anonymous$$g for 1 week :s)
Here is my code, #pragma strict // These variables are for adjusting in the inspector how the object behaves var maxSpeed = 8.000; var force = 20.000; var jumpSpeed =5.000; //Eulers Var var yRotation : float = 20.0; // These variables are there for use by the script and don't need to be edited private var state = 0; private var grounded = false; private var jumpLimit = 0; //--------------------------------------------------------------------------------- // Don't let the Physics Engine rotate this physics object so it doesn't fall over when running function Awake () { rigidbody.freezeRotation = true; } //--------------------------------------------------------------------------------- // This part detects whether or not the object is grounded and stores it in a variable function OnCollisionEnter () { state ++; if(state > 0) { grounded = true; } } //--------------------------------------------------------------------------------- function OnCollisionExit () { state --; if(state < 1) { grounded = false; state = 0; } } //--------------------------------------------------------------------------------- // This is called every physics frame function FixedUpdate () { // Get the input and set variables for it var jump = Input.GetButtonDown ("Jump"); var horizontal = Input.GetAxis("Horizontal"); var vertical = Input.GetAxis("Vertical"); // Set the movement input to be the force to apply to the player every frame horizontal *= force; vertical *= force; // If the object is grounded and isn't moving at the max speed or higher apply force to move it if(rigidbody.velocity.magnitude < maxSpeed && grounded == true) { rigidbody.AddForce (transform.rotation * Vector3.forward * vertical); rigidbody.AddForce (transform.rotation * Vector3.right * horizontal); } // This part is for jumping. I only let jump force be applied every 10 physics frames so // the player can't somehow get a huge velocity due to multiple jumps in a very short time if(jumpLimit < 1) jumpLimit ++; if(jump && grounded == true && jumpLimit >= 1) { rigidbody.velocity.y += jumpSpeed; jumpLimit = 1; } } //--------------------------------------------------------------------------------- //Problema Relacionado con los frames, mover por tiempo / $$anonymous$$ovimiento de La esfera del dragon function Update (){ $$anonymous$$ovement(); } //--------------------------------------------------------------------------------- function $$anonymous$$ovement(){ transform.eulerAngles = Vector3(transform.localEulerAngles.x, Camera.main.transform.localEulerAngles.y, transform.localEulerAngles.z); } //---------------------------------------------------------------------------------
Sorry for the bad format
pragma strict
// These variables are for adjusting in the inspector how the object behaves var maxSpeed = 8.000; var force = 20.000; var jumpSpeed =5.000;
//Eulers Var
var yRotation : float = 20.0;
// These variables are there for use by the script and don't need to be edited
private var state = 0;
private var grounded = false;
private var jumpLimit = 0;
//---------------------------------------------------------------------------------
// Don't let the Physics Engine rotate this physics object so it doesn't fall over when running
function Awake ()
{
rigidbody.freezeRotation = true;
}
//---------------------------------------------------------------------------------
// This part detects whether or not the object is grounded and stores it in a variable
function OnCollisionEnter ()
{
state ++;
if(state > 0)
{
grounded = true;
}
}
//---------------------------------------------------------------------------------
function OnCollisionExit ()
{
state --;
if(state < 1)
{
grounded = false;
state = 0;
}
}
//---------------------------------------------------------------------------------
// This is called every physics frame
function FixedUpdate ()
{
// Get the input and set variables for it
var jump = Input.GetButtonDown ("Jump");
var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
// Set the movement input to be the force to apply to the player every frame
horizontal *= force;
vertical *= force;
// If the object is grounded and isn't moving at the max speed or higher apply force to move it
if(rigidbody.velocity.magnitude < maxSpeed && grounded == true)
{
rigidbody.AddForce (transform.rotation * Vector3.forward * vertical);
rigidbody.AddForce (transform.rotation * Vector3.right * horizontal);
}
// This part is for jumping. I only let jump force be applied every 10 physics frames so
// the player can't somehow get a huge velocity due to multiple jumps in a very short time
if(jumpLimit < 1) jumpLimit ++;
if(jump && grounded == true && jumpLimit >= 1)
{
rigidbody.velocity.y += jumpSpeed;
jumpLimit = 1;
}
}
//---------------------------------------------------------------------------------
//Problema Relacionado con los frames, mover por tiempo / $$anonymous$$ovimiento de La esfera del dragon
function Update (){
$$anonymous$$ovement();
}
//---------------------------------------------------------------------------------
function $$anonymous$$ovement(){
transform.eulerAngles = Vector3(transform.localEulerAngles.x, Camera.main.transform.localEulerAngles.y, transform.localEulerAngles.z);
}
//---------------------------------------------------------------------------------
The code you posted appears to be for the character. What I'm wondering is how you are going to move the Quad. Is it just 2D or a 3D rotation? What Inputs define the rotation? BTW, a Quad is a basic game object. You can create one by:
GameObject > Create Other > Quad
It is just a two triangle plane with a natural vertical orientation and a collider.
It is 3d rotation.
I think I have found the solution, I modified the orbit script prefab, and just substracted 180 degrees so that it tilts in front of the character XD
var target : Transform;
var distance = 20.0;
var xSpeed = 150.0;
var ySpeed = 120.0;
var y$$anonymous$$inLimit = 25;
var y$$anonymous$$axLimit = 60;
private var x = 0.0;
private var y = 0.0;
function Start () {
transform.position = Vector3(0,10,0);
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// $$anonymous$$ake the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target) {
x += Input.GetAxis("$$anonymous$$ouse X") * xSpeed * 0.01;
y -= Input.GetAxis("$$anonymous$$ouse Y") * ySpeed * 0.01;
y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
var rotation = Quaternion.Euler(y, x+180, 0);
var position = rotation * Vector3(0.0, 0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static function ClampAngle (angle : float, $$anonymous$$ : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max);
Your answer
![](https://koobas.hobune.stream/wayback/20220613143434im_/https://answers.unity.com/themes/thub/images/avi.jpg)