There is an cube wich scales with the distance.magnitude between the player and the enemy. I want to set this Scaling Cube in the middle between the hero and the enemy. So is it possible to use the mangitude between two objects as a position.
This is my script:
var hero : Transform;
var enemy : Transform;
var magDistance = 0.0;
var setPosition = 0.0;
function Update () {
var heDistance : Vector3 = (hero.position - enemy.position)/2;
magDistance = heDistance.magnitude;
setPosition = heDistance.magnitude/2;
transform.localScale = Vector3(1,1,magDistance);
}
Im using the heDistance.magnitude/2 to get the middle of the distance.
Help is much appreciated. Thanks in advance! :)
I hate to even ask, but can't you just do:
x = (hero.position.x+enemy.position.x)/2
y = (hero.position.y+enemy.position.y)/2
z = (hero.position.z+enemy.position.z)/2
to get the point between the two points? Or, if vector addition works in the usual way:
var cube_pos: Vector3 = (hero.position + enemy.position)/2;
Related
The code I am using currently makes the enemy notice me at a distance then follow me if I get closer. The issue I am having is with how they move. I am building a Minecraft style game but I cant get the enemies to stay on the ground and jump up each block like I have too with the fps controller. They just seem to float the shortest distance possible towards me.
Code:
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var range : float=10f;
var range2 : float=10f;
var stop : float=0;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("1Player").transform; //target the player
}
function Update () {
//rotate to look at the player
var distance = Vector3.Distance(myTransform.position, target.position);
if (distance<=range2 && distance>=range){
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
else if(distance<=range && distance>stop){
//move towards the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance<=stop) {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
}
Okay now here is what you are missing:
1.Lock the rotation on x and z axis to zero
myTransform.eulerAngles = new Vector3(0f, myTransform.eulerAngles.y, 0);
Just add this line at the end.
2.Make a short distance raycast in front of the AI and if it detects some obstacle, stop the current movement and move by y axis, if there is no obstacle move like you wrote in your script.
As for the raycasting goes, there is no need for me to write it here, you can find more info about it in Unity3D documentation here:
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html and this tutorial is good for staters: https://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting
I am having an issue with my code, i'm trying to move a 3D box to the variable of the position of the mouse, I need to know how to change the box's x,y,z with my mouse position script.
All im asking really, is how do I change my boxes x,y,z with a variable in another script. Thanks!
Code:
#pragma strict
public var distance : float = 4.5;
var box = Transform;
private var firstObject : cube;
function Start () {
}
function Update () {
CastRayToWorld();
}
function CastRayToWorld() {
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var point : Vector3 = ray.origin + (ray.direction * distance);
Debug.Log( "World point " + point );
firstObject = GameObject.Find("pos").GetComponent("cube").pos = point;
firstObject.pos = point;
}
Make sure that the other object is aware of your box gameObject (lets say under the name 'adjustable'), then its simply a case of:
adjustable.transform.position = new Vector3(x, y, z)
To make sure that the object is aware of the boxes gameObject, you could make adjustable a public variable, and then manual drag the box from your scene into the field that would be created in the component on the object in question.
in unity i made a script for my guy to crouch, run , and have his regular speed but when i crouch i just fall forever into the terrain was wondering if it is my code or i have to put a check on the terrain.
var walkSpeed: float = 7; // regular speed
var crchSpeed: float = 3; // crouching speed
var runSpeed: float = 20; // run speed
private var chMotor: CharacterMotor;
private var ch: CharacterController;
private var tr: Transform;
private var height: float; // initial height
function Start(){
chMotor = GetComponent(CharacterMotor);
tr = transform;
ch = GetComponent(CharacterController);
height = ch.height;
}
function Update(){
var h = height;
var speed = walkSpeed;
if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
speed = runSpeed;
}
if (Input.GetKey("c")){ // press C to crouch
h = 0.5 * height;
speed = crchSpeed; // slow down when crouching
}
chMotor.movement.maxForwardSpeed = speed; // set max speed
var lastHeight = ch.height; // crouch/stand up smoothly
ch.height = Mathf.Lerp(ch.height, h, 5*Time.deltaTime);
tr.position.y += (ch.height-lastHeight)/2; // fix vertical position
}
This line could be responsible:
tr.position.y += (ch.height-lastHeight)/2;
you might want to let gravity take care of this or add a downward Force here. Changing the transform directly makes the Object dont care about colliders in the way.
edit: a better way would probably be to change the CharacterController though, as "falling down" when crouching doesnt make much sense. Maybe you dont even need this? The way i would implement this is the root at the feet, and height goes upwards from there.
Hello i followed a tutorial on how to make a top down shooter, the code makes so that my character rotates against my mouse and i can move forward and backwards using W and S. However i also want to be able to move right and left using A and D im not good when it comes to code in javascript nor unity3d i do most of my coding in c#. And the guy that made the tutorial explained poorly what some of the code actually do.
Here is the code:
#pragma strict
var speed : float = 20.0;
var rotateSpeed : float = 2.0;
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed,0);
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed : float = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curSpeed);
var position = Input.mousePosition;
var newposition = Vector3(position.x,position.y,camera.main.transform.position.y- transform.position.y);
var lastposition = camera.main.ScreenToWorldPoint(newposition);
transform.LookAt(lastposition);
}
#script RequireComponent(CharacterController)
code for staffing (moving left and right without turning)
if(Input.GetKeyUp("d")){
controller.SimpleMove(right * staffingSpeed);
}
else if(Input.GetKeyUp("a")) {
controller.SimpleMove(-right * staffingSpeed);
}
write the code in update() function and make a variable "staffingSpeed" outside the function and set it from inspector
or
replace "staffingSpeed" with a constant numeric value e.g 5
I am trying to make an Object move to a Cube wich is stored in an Array.
The Array is filled with gameObjects with a tag.
I can get the Object to move instantly to the cube, but not slowly like its walking to it.
This is my script:
var moveTo : GameObject;
function Update(){
print(FindClosestEnemy().name);
}
function FindClosestEnemy():GameObject{
var chasePoints : GameObject[];
chasePoints = GameObject.FindGameObjectsWithTag("chasePoint");
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
for(var go: GameObject in chasePoints){
var diff = (go.transform.position-position);
var curDistance = diff.sqrMagnitude;
if(curDistance < distance){
closest = go;
moveTo = closest;
transform.position -= moveTo.transform.position;
distance = curDistance;
}
}
return closest;
}
I also tried the Time.deltaTime thing, but then it teleports far away from the cube.
And just converting it to Transform Array isnt working out either :( Any ideas to make this work?
Help is much appreciated :) Thanks in advance!
It looks like you are doing the whole transformation in one frame. You need to pick a velocity and apply it to the object for each frame. You know the from and to. Pick how long you want it to take and update it incrementally. Remember that you cannot be sure there will be 60 frames a second so take that into account.
You can use
Vector3.MoveTowards
For maxDistanceDelta you choose a velocity and multiply it with Time.deltaTime to be frame-length-independant:
Vector3.MoveTowards(transform.position, moveTo.transform.position, speed * Time.deltaTime);