- Home /
Question by
OmarComin · Apr 28, 2012 at 04:42 PM ·
updateloopquaternion.slerp
Loop Logic - Update() and Quarternion.Slerp
Hopefully it's obvious what the problem is in the code. Can't quite figure out how to get around it. The object rotates how I want but only a incrementally because the loop then waits for (Input.GetMouseButtonDown(1) again on the next frame which stops the rotation. Any ideas?
void Update () {
if (Input.GetMouseButtonDown(1)){
GameObject[] sl;
sl = GameObject.FindGameObjectsWithTag("White");
foreach(GameObject slt in sl) {
UnitStats us = (UnitStats)slt.GetComponent("UnitStats");
if (us.selected == true && us.movementphase == 0) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.transform.tag == "Ground"){
moveTo = hit.point;
UnitSelect rs = (UnitSelect)GetComponent("UnitSelect");
moveFrom = rs.selectedTarget.transform;
Debug.Log(moveTo);
moveTo.y = moveFrom.position.y;
moveFrom.rotation = Quaternion.Slerp(moveFrom.rotation, Quaternion.LookRotation(moveTo - moveFrom.position), rotationSpeed * Time.deltaTime);
}
}
}
}
}
Comment
lol that ended up formatted kinda weird... but ah'well, at least it's all in a code-block now :)
Answer by OmarComin · Apr 30, 2012 at 12:48 AM
Here's how I got it working how I wanted in the end:
using UnityEngine;
using System.Collections;
public class UnitMoveTo : MonoBehaviour {
public Transform unitTransform;
public Vector3 moveLeash;
public Vector3 moveTo;
public int moveSpeed;
public int rotationSpeed;
public float distance;
public float maxDistance;
public int movephase;
void Awake () {
unitTransform = transform;
moveTo = unitTransform.position;
moveLeash = unitTransform.position;
}
void Update () {
UnitStats us = (UnitStats)unitTransform.GetComponent("UnitStats");
distance = Vector3.Distance (moveTo, unitTransform.position);
maxDistance = us.move;
if ((distance < maxDistance) && (moveTo != unitTransform.position) && distance > 0.5){
us.movementphase = 1;
//rotate and move from moveFrom to moveTo
unitTransform.rotation = Quaternion.Slerp(unitTransform.rotation, Quaternion.LookRotation(moveTo - unitTransform.position), rotationSpeed * Time.deltaTime);
unitTransform.position += unitTransform.forward * moveSpeed * Time.deltaTime;
}
if (us.selected == true && us.movementphase == 0 && Input.GetMouseButtonUp(1)) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.transform.tag == "Ground") {
moveTo.x = hit.point.x;
moveTo.z = hit.point.z;
}
}
if (us.selected == true && us.movementphase == 0 && Input.GetKeyUp(KeyCode.Space)){us.movementphase = 1;}
}
}
Your answer
Follow this Question
Related Questions
How to slow down large code? 1 Answer
How to call unity event once every time my object reach destination in Update()? 1 Answer
Update List Item 0 Answers
c# update loop. How to? 2 Answers