var Distance;
var Target = transform;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance > lookAtDistance)
{
}
if (Distance < attackRange)
{
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position -
transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation,
Time.deltaTime * Damping);
}
function attack ()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
I am trying to make my object go towards the player but i cant put anything in the slot where it prompts you to put the object i want it to follow
the problem i believe is at line 2 but i have tried so many things and i cant do anything to fix it
i would like to note i have another piece of code simialr to this in a different script and it works just fine.
Replace var Target = transform; with var Target:Transform; on line 2.
Related
I have this script, that already works
I,m using leanFinger
var c = Camera.main;
var center = rb.position;
var lastPos = _finger.GetWorldPosition(c.transform.position.y, c);
var lastDelta = Vector3.Distance(center, lastPos);
float angle = Mathf.Atan2(lastPos.x, lastPos.z) * Mathf.Rad2Deg;
rb.rotation = Quaternion.Euler(new Vector3(0, angle - initialRotation, 0));
The problem is that my object is rotating in absolute angles, so if it is already rotated, there is a unwanted rotation. I want it to take an initialRotation value, but i don't know how.
Thanks
You can use transform.rotation to access the current rotation of your object: Transform.rotation
You could also use this method for convenience: Transform.LookAt
Simple JS example:
public var target : Transform;
function Update ()
{
transform.LookAt(target);
}
I've been working on a project in Unity3D and found a javascript that makes the enemies follow the player. This is the script:
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
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("Player").transform; //target the player
}
function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
When the enemies "rotate to look at the player" they rotate from a horizontal position to a vertical. How can I change the script in order to prevent that from happening?
You have a secon parameter at Quaternion.LookRotation
You may use :
Quaternion.LookRotation(target.position - myTransform.position, Vector3.up)
or
Quaternion.LookRotation(target.position - myTransform.position, transform.up)
This is the code for my player's movement:
var animator : Animator;
function Start () {
animator = GetComponent("Animator");
}
var moveUp : KeyCode;
var moveDown : KeyCode;
var moveLeft : KeyCode;
var moveRight : KeyCode;
var jumpheight : int = 2;
var speed : int = 2;
var isGrounded : int = 0;
// Checks if player is in air or ground
var Jumptest : int = 0;
// Checks if player has jumped
function Update () {
animator.SetInteger("Direction", 4);
if (Input.GetKey(moveUp) && isGrounded == 0)
{
rigidbody2D.velocity.y = jumpheight;
isGrounded = 1;
animator.SetInteger("Direction", 2);
}
else if (Input.GetKey(moveLeft))
{
transform.position += Vector3.left * speed * Time.deltaTime;
animator.SetInteger("Direction", 3);
}
else if (Input.GetKey(moveRight))
{
transform.position += Vector3.right * speed * Time.deltaTime;
animator.SetInteger("Direction", 1);
}
}
and this is the code for the projectiles movement:
#pragma strict
var speed = 8.0;
var time = 0.0;
function Start () {
}
var moveRight : KeyCode;
var moveLeft : KeyCode;
function Update () {
time += Time.deltaTime;
if(time > 3){
Destroy(gameObject);
}
transform.Translate(Vector3.right * speed * Time.deltaTime, Space.Self);
}
At the moment, the projectiles shoot to the right. I want to be able to shoot them in the direction the player is facing.
I tried if key down a and if key down d but then the projectiles stop when the player is not moving and move relative to the player (if I shoot one going left, then move my player to the right, the projectile reverses direction).
Anybody know?
When checking for player movement , whether the left/right key is down, you create a simple integer which keeps track of direction
...
var isRight: int=1 ;//assuming player starts with facing in the right direction
...
and then, when checking for key press set the value of this variable appropriately
...
else if (Input.GetKey(moveLeft)&&!Input.GetKey(moveRight))
{
isRight = -1;
...
}
else if (Input.GetKey(moveRight)&&!Input.GetKey(moveLeft))
{
isRight = 1;
...
}
Finally when setting translation for the projectile simply multiply the speed with this new variable
transform.Translate(Vector3.right * speed * isRight * Time.deltaTime, Space.Self);
I have an orthographic camera and would like to implement a zoom feature to a specific point. I.e., imagine you have a picture and want to zoom to a specific part of the picture.
I know how to zoom in, the problem is to move the camera to a position that has the desired zone in focus.
How can I do so?
The camera's orthographicSize is the number of world space units in the top half of the viewport. If it's 0.5, then a 1 unit cube will exactly fill the viewport (vertically).
So to zoom in on your target region, center your camera on it (by setting (x,y) to the target's center) and set orthographicSize to half the region's height.
Here is an example to center and zoom to the extents of an object. (Zoom with LMB; 'R' to reset.)
public class OrthographicZoom : MonoBehaviour
{
private Vector3 defaultCenter;
private float defaultHeight; // height of orthographic viewport in world units
private void Start()
{
defaultCenter = camera.transform.position;
defaultHeight = 2f*camera.orthographicSize;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Collider target = GetTarget();
if(target != null)
OrthoZoom(target.bounds.center, target.bounds.size.y); // Could directly set orthographicSize = bounds.extents.y
}
if (Input.GetKeyDown(KeyCode.R))
OrthoZoom(defaultCenter, defaultHeight);
}
private void OrthoZoom(Vector2 center, float regionHeight)
{
camera.transform.position = new Vector3(center.x, center.y, defaultCenter.z);
camera.orthographicSize = regionHeight/2f;
}
private Collider GetTarget()
{
var hit = new RaycastHit();
Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit);
return hit.collider;
}
}
hope you find this code example useful, should be a copy / paste.
Note: this script assume it's attached to the camera object, otherwise you should adjust the transform to the camera object reference.
private float lastZoomDistance = float.PositiveInfinity; // remember that this should be reset to infinite on touch end
private float maxZoomOut = 200;
private float maxZoomIn = 50;
private float zoomSpeed = 2;
void Update() {
if (Input.touchCount >= 2) {
Vector2 touch0, touch1;
float distance;
Vector2 pos = new Vector2(transform.position.x, transform.position.y);
touch0 = Input.GetTouch(0).position - pos;
touch1 = Input.GetTouch(1).position - pos;
zoomCenter = (touch0 + touch1) / 2;
distance = Vector2.Distance(touch0, touch1);
if(lastZoomDistance == float.PositiveInfinity) {
lastZoomDistance = distance;
} else {
if(distance > lastZoomDistance && camera.orthographicSize + zoomSpeed <= maxZoomOut) {
this.camera.orthographicSize = this.camera.orthographicSize + zoomSpeed;
// Assuming script is attached to camera - otherwise, change the transform.position to the camera object
transform.position = Vector3.Lerp(transform.position, zoomCenter, Time.deltaTime);
} else if(distance < lastZoomDistance && camera.orthographicSize - zoomSpeed >= maxZoomIn) {
this.camera.orthographicSize = this.camera.orthographicSize - zoomSpeed;
transform.position = Vector3.Lerp(transform.position, zoomCenter, Time.deltaTime);
}
}
lastZoomDistance = distance;
}
}
I have an object and 2 GUI texture buttons. I want to rotate the object to the left when I press the left button and to the right while pressing the other one.
Any ideas ?
I have a script that works when I drag my object. I will post the important part:
function Start ()
{
var angles = transform.eulerAngles;
x = angles.y;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate ()
{
if (isMouseOverGuiTexture()) return;
if (target && Input.GetMouseButton(0))
{
//0.1 represents the sensitivity of the mouse
x += Input.GetAxis("Mouse X") * xSpeed *0.1; //x rotation
//y -= Input.GetAxis("Mouse Y") * ySpeed *0.1; //y rotation
//y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.900528, 8.829305, -distance+0.49548)+ target.position;
transform.rotation = rotation;
transform.position = position;
}
}
(Question answered in the comments. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
I solved it by using the following :
var imageLeft: Texture2D; // drag the left button image here
var imageRight: Texture2D; // right button image
var speed: float = 60; // rotate speed in degrees per second
private var rotLeft = false; // object rotates left if true
private var rotRight = false; // object rotates right if true;
function OnGUI()
{
rotLeft = GUI.RepeatButton(Rect(10,10,200,200), imageLeft);
rotRight = GUI.RepeatButton(Rect(230,10,200,200), imageRight);
}
function Update()
{
if (rotLeft) transform.Rotate(0, -speed*Time.deltaTime, 0);
if (rotRight) transform.Rotate(0, speed*Time.deltaTime, 0);
}
I don't know which value Time.deltaTime assumes inside OnGUI - it should be the time since last OnGUI, but I'm not sure. To avoid problems, I placed the real rotation in Update and used two control booleans (rotLeft and rotRight) to communicate with OnGUI.