Im trying to do a coroutine that sets the character to hurt mode for a persiod. During this period i want it to flash. Im not familiar with modulus but i tried to use it to flip the sprite renderer every 10 or so cycles. But i get really weird values, is it cause its a float? How should i do this in unity?
public IEnumerator Hurt_cr (float sec, Vector2 attackPosition, float force)
{
isHurt = true;
AddForceAtPosition (attackPosition, force);
SpriteRenderer sprite = GetComponent< SpriteRenderer> ();
float t = Time.deltaTime;
while (t < sec) {
if ((t % 10) == 0) {
print ("sprite on : " + sprite.enabled);
sprite.enabled = !sprite.enabled;
}
yield return null;
t += Time.deltaTime;
}
isHurt = false;
}
Don't use a coroutine, it's totally useless. Put this into Update:
sprite.enabled = ((int)(Time.time / 10)) % 2 == 0;
EDIT: added (int) cast for good measure.
EDIT 2: to have it flash a fixed number of times
Add the class propery:
public int flashes = 0;
private bool flashingLastFrame = false;
In Update count the flashes, if any:
bool flashingNow;
if (flashes > 0)
{
flashingNow = ((int)(Time.time / 10)) % 2 == 0;
if (flashingNow != flashingLastFrame)
{
flashingLastFrame = flashingNow;
sprite.enabled = flashingNow;
flashes--; // forgot this to make it stop
}
}
To have it flash 3 times set from the outside:
obj.flashes = 6;
Or add a convenience method:
public void Flash(int times)
{
flashes = times * 2;
}
EDIT 3: For super-fast flashing:
if (flashes > 0)
{
flashingLastFrame = !flashingLastFrame;
sprite.enabled = flashingLastFrame;
flashes--;
}
Related
I am trying to make enemy patrolling system, where evrytime guard reaches his point, he stopes for 10 seconds, and then continue his movement. I've tried combining animations from Blend tree with isStopped property from NavMeshAgent.
EDIT: My current script makes agent move to point, then he stopes for some time, and then only walk animation plays, but he staing on one place.
public Transform[] points;
private int destPoint = 0;
public NavMeshAgent agent;
public Animator animator;
public int time;
void Start()
{
agent = GetComponent<NavMeshAgent>();
animator = transform.Find("Enemy").GetComponent<Animator>();
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
//agent.autoBraking = false;
}
void GotoNextPoint()
{
// Returns if no points have been set up
if (points.Length == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Length;
//agent.speed = 1f;
//animator.SetFloat("Blend", agent.speed);
}
void Update()
{
if (agent.remainingDistance == 0f && time == 100000)
{
agent.speed = 1f;
Debug.Log(agent.remainingDistance);
animator.SetFloat("Blend", 1);
GotoNextPoint();
}
else if (agent.remainingDistance <= 0.5f && agent.remainingDistance != 0f && time == 100000)
{
animator.SetFloat("Blend",0);
agent.enabled = false;
GotoNextPoint();
}
else if(animator.GetFloat("Blend") == 0)
{
time--;
}
if (time == 99000 && animator.GetFloat("Blend") == 0)
{
time = 10000;
agent.enabled = true;
agent.isStopped = false;
animator.SetFloat("Blend", 1);
agent.autoRepath = true;
GotoNextPoint();
}
}
I changed few lines of code, now agent moves after first stop, but second time he stops at second poitm,walking animation still working, time doesn't decrementing
if (time == 99000 && animator.GetFloat("Blend") == 0)
{
time = 10000;
agent.enabled = true;
agent.isStopped = false;
animator.SetFloat("Blend", 1);
//New lines of code
agent.ResetPath();
destPoint = (destPoint + 1) % points.Length;
agent.SetDestination(points[destPoint].position);
}[enter image description here][1]
First of all, I would use the "SetDestination" function in order to set the next destination.
In the end you wrote:
if (time == 99000 && animator.GetFloat("Blend") == 0)
{
time = 10000; *-> needs to be 100000, not 10000*
agent.enabled = true;
agent.isStopped = false;
animator.SetFloat("Blend", 1);
agent.autoRepath = true;
GotoNextPoint();
}
You can use "NavMeshAgent.ResetPath" to reset the path instead of using "autoRepath".
After the "ResetPath", use the "SetDestination" inside the function GotoNextPoint().
One more very important thing - Check that there is more than one point.
If there is just one point, your guard will just walk at the same spot.
For more information, is suggest to check out Unity NavMeshAgent
The question says it all, I have a Coroutine that I am using to create some spinning animation. The problem is that it suddenly stops randomly (at different times). I don't know what might be causing this as it works most of the times (8 times out of 10) on PC. I also built the game and tried it on an android phone but it only works (3 times out of 10). Any idea what might be causing this?
This is my Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpinWheelScript : MonoBehaviour {
public CanvasGroup spinWheelCanvas;
public GameObject[] rewards;
public GameObject[] pointerDir;
public GameObject[] ringDir;
public GameObject pointer;
public GameObject ring;
private Quaternion pointerTargetRotation;
private Quaternion ringTargetRotation;
private bool spinIsEnabled = false;
private bool isSpinning = false;
private bool lastSpin = false;
private bool animationIsEnabled = false;
private float time;
private float pSpeed;
private float rSpeed;
private float pointerRotateFloat;
private float ringRotateFloat;
private int rewardEnabler = 0;
private int randomReward;
private int pRandomDir;
private int rRandomDir;
private int plastRandomDir = 0;
private int rlastRandomDir = 0;
private void Update()
{
if (spinIsEnabled == false)
{
// Do Nothing
}
else if (spinIsEnabled == true && isSpinning == false)
{
// Select reward
if (rewardEnabler == 0)
{
RewardSelector();
rewardEnabler = 1;
}
StartCoroutine(Spin());
}
if (lastSpin == true)
{
Debug.Log(randomReward);
pointerRotateFloat = ((360 - (randomReward * 60)) - 30);
ringRotateFloat = ((360 - (randomReward * 60)) - 30);
if (pointerRotateFloat > ringRotateFloat)
{
pSpeed = (pointerRotateFloat);
rSpeed = (pointerRotateFloat / ringRotateFloat) * (ringRotateFloat);
}
else
{
rSpeed = (ringRotateFloat);
pSpeed = (ringRotateFloat / pointerRotateFloat) * (pointerRotateFloat);
}
Quaternion pointerTargetRotation = Quaternion.Euler(new Vector3(0, 0, pointerRotateFloat));
pointer.transform.rotation = Quaternion.RotateTowards(pointer.transform.rotation, pointerTargetRotation, pSpeed * Time.deltaTime);
Quaternion ringTargetRotation = Quaternion.Euler(new Vector3(0, 0, ringRotateFloat));
ring.transform.rotation = Quaternion.RotateTowards(ring.transform.rotation, ringTargetRotation, rSpeed * Time.deltaTime);
if ((pointer.transform.rotation == pointerTargetRotation) && (ring.transform.rotation == ringTargetRotation))
{
lastSpin = false;
isSpinning = false;
spinIsEnabled = false;
animationIsEnabled = true;
}
}
}
IEnumerator Spin()
{
isSpinning = true;
pSpeed = 0;
rSpeed = 0;
time = 0;
while (time < 15)
{
pRandomDir = PointerRandomDirection(); // Function to pick a random number.
rRandomDir = RingRandomDirection(); // Function to pick a random number.
for (;;)
{
pointerRotateFloat = (((pRandomDir + 1) * 60) - 30) - pointer.transform.rotation.z;
ringRotateFloat = (((rRandomDir + 1) * 60) - 30) - ring.transform.rotation.z;
if (pointerRotateFloat > ringRotateFloat)
{
pSpeed = (pointerRotateFloat);
rSpeed = (pointerRotateFloat / ringRotateFloat) * (ringRotateFloat);
}
else
{
rSpeed = (ringRotateFloat);
pSpeed = (ringRotateFloat / pointerRotateFloat) * (pointerRotateFloat);
}
pointerTargetRotation = Quaternion.Euler(new Vector3(0, 0, pointerRotateFloat));
pointer.transform.rotation = Quaternion.RotateTowards(pointer.transform.rotation, pointerTargetRotation, pSpeed * Time.deltaTime);
ringTargetRotation = Quaternion.Euler(new Vector3(0, 0, ringRotateFloat));
ring.transform.rotation = Quaternion.RotateTowards(ring.transform.rotation, ringTargetRotation, rSpeed * Time.deltaTime);
Debug.Log("Before the if");
if ((pointer.transform.rotation == pointerTargetRotation) && (ring.transform.rotation == ringTargetRotation))
break;
yield return null;
}
time++;
}
lastSpin = true;
}
private int RewardSelector()
{
randomReward = Random.Range(0, rewards.Length);
return randomReward;
}
private int PointerRandomDirection()
{
int pRandomDir = plastRandomDir;
if (pointerDir.Length <= 1)
return 0;
while (pRandomDir == plastRandomDir)
{
pRandomDir = Random.Range(0, pointerDir.Length);
}
plastRandomDir = pRandomDir;
return pRandomDir;
}
private int RingRandomDirection()
{
int rRandomDir = rlastRandomDir;
if (ringDir.Length <= 1)
return 0;
while (rRandomDir == rlastRandomDir)
{
rRandomDir = Random.Range(0, ringDir.Length);
}
rlastRandomDir = rRandomDir;
return rRandomDir;
}
public void OnSpinButtonClick()
{
if(spinIsEnabled == false && isSpinning == false)
spinIsEnabled = true;
spinWheelCanvas.interactable = false;
}
}
Thanks in advance.
If you don't find the answer using your existing solution, here is an alternative solution you might appreciate, which arguably is more simple.
A simpler way to achieve the spinning effect and stopping at random angles:
Make 2 animations (In the Animation/Animator windows), a 0-180 degree animation and a 180-360 degree animation, then have them transition between each other.
Making just one 0-360 animation will most likely look buggy, hence I advise to make it in 2 parts.
You can then decide the speed of this animation by adjusting the animation speed and Animator parameters (which control what animations are playing) through code.
To have the spinning wheel stop at random angles you could have a float value in the animator called "Speed", the higher the speed the longer time it takes to stop the animation, hence random stopping angles are achieved.
When the speed hits 0, set the animation speed to 0 as well, that should stop the animation at the designated angle.
Feel free to ask questions if you don't understand what I mean.
I'm sorry the question might be a little weird but I don't know how to ask this otherwise.
I have been developing an endless runner game, and downloaded this great asset package from store. It will bend the road or tiles left or right according to the desired amount.
I have a set of numbers in my mind -10, 0 and 10,
10 will bend the tiles to the left. (according to the asset, (+) is left.)
0 will do nothing, it will be straight tiles.
-10 will bend the tiles to the right
Right now I have developed it to gradually increase the curve to the left and stop at 10.
This is the code.
float curve = 0;
float curTarget = 10;
void Update ()
{
if (curve <= curTarget)
{
curve = curve + (Time.deltaTime * .5f);
}
else if (curve == curTarget)
{
HB.ApplyCurvature(curve); //method called from the package.
}
HB.ApplyCurvature(curve); //method called from the package
}
So my question is how to increase and decrease those values between each other (-10, 0, 10). Basically I want the pattern to be;
decrease gradually from 0 to -10 waitForSeconds(5)
Increase gradually from -10 to 0 waitForSeconds(5)
Increase gradually from 0 to 10 waitForSeconds(5)
Finally then decrease from 10 to 0 waitForSeconds(5) and then from top again.
How do I achieve such a scenario, I tried to use a switch but it didn't work out.
This is also same as the subway surfers. It beautifully go through with this pattern. I'm bit new to unity. Help would be greatly appreciated.
As new Unity progammer, it is important that you understand how to use coroutine and the lerp function. This is one example where it is needed. How to move object over time... This has been asked many times, so Googling them should yield a great result and get you started. You must know those in order to do this.
bool keepRunning = false;
IEnumerator pattern()
{
while (keepRunning)
{
float mainPatternValue = 0;
//decrease gradually from 0 to -10 waitForSeconds(5)
const float patternDuration = 5f;
float counter = 0;
while (counter < patternDuration)
{
if (!keepRunning)
yield break;
counter += Time.deltaTime;
mainPatternValue = Mathf.Lerp(0, -10, counter / patternDuration);
Debug.Log("<color=red>Decr from 0 to -10: " + mainPatternValue + "</color>");
yield return null;
}
Debug.Log("<color=yellow>DONE</color>");
//Increase gradually from -10 to 0 waitForSeconds(5)
counter = 0;
mainPatternValue = -10;
while (counter < patternDuration)
{
if (!keepRunning)
yield break;
counter += Time.deltaTime;
mainPatternValue = Mathf.Lerp(-10, 0, counter / patternDuration);
Debug.Log("<color=green>Incr from -10 to 0: " + mainPatternValue + "</color>");
yield return null;
}
Debug.Log("<color=yellow>DONE</color>");
//Increase gradually from 0 to 10 waitForSeconds(5)
counter = 0;
mainPatternValue = 0;
while (counter < patternDuration)
{
if (!keepRunning)
yield break;
counter += Time.deltaTime;
mainPatternValue = Mathf.Lerp(0, 10, counter / patternDuration);
Debug.Log("<color=red>Incr from 0 to 10: " + mainPatternValue + "</color>");
yield return null;
}
Debug.Log("<color=yellow>DONE</color>");
//Finally then decrease from 10 to 0 waitForSeconds(5) and then from top again
counter = 0;
mainPatternValue = 10;
while (counter < patternDuration)
{
if (!keepRunning)
yield break;
counter += Time.deltaTime;
mainPatternValue = Mathf.Lerp(10, 0, counter / patternDuration);
Debug.Log("<color=green>Decr from 10 to 0: " + mainPatternValue + "</color>");
yield return null;
}
Debug.Log("<color=yellow>Finally DONE</color>");
Debug.Log("<color=yellow>Starting Over Again!</color>");
yield return null;
}
}
void start()
{
if (keepRunning)
return;
keepRunning = true;
StartCoroutine(pattern());
}
void stop()
{
keepRunning = false;
}
To start the coroutine call start(). To stop it call stop().
I'm on mobile so I can't test it but hopefully this helps...
Use this function to return a value to increment/decrement the current curvature by based on the difference between the target curve and the current curve.
It currently uses just Time.deltaTime as the increment. I added in a commented out curve speed apply line, just remove the /* and */ and give curveSpeed a value to make use of it.
float Bend(float current, float target) {
//Don't bend if current curve is equal to target curve
if (current == target)
return 0;
//Find the sign of the difference between current and target
float sgn = (target - current) / Math.Abs(target - current);
//Apply the sign to the incremental value
float val = /*curveSpeed * */Time.deltaTime * sgn;
//If the absolute value of the increment/decrement + the current value is greater than
//the absolute value of the target, only return the difference (prevents over curving)
if (Mathf.Abs(current + val) > Mathf.Abs(target)) {
return target-current;
}
//Return the incremental/decremental value
return val;
}
So an example of implementing this in a pattern within update:
enum Pattern { a, b, c, d }
Pattern pattern;
float curve = 0;
float curveSpeed = 2;
void Update() {
switch (pattern) {
case Pattern.a:
HB.ApplyCurvature(Bend(curve, -10));
if (Bend(curve, -10) == 0) pattern = Pattern.b;
break;
case Pattern.b:
HB.ApplyCurvature(Bend(curve, 0));
if (Bend(curve, 0) == 0) pattern = Pattern.c;
break;
case Pattern.c:
HB.ApplyCurvature(Bend(curve, 10));
if (Bend(curve, 10) == 0) pattern = Pattern.d;
break;
case Pattern.d:
HB.ApplyCurvature(Bend(curve, 0));
if (Bend(curve, 0) == 0) pattern = Pattern.a;
break;
}
}
The only thing missing is the delay you mentioned.
I have been working on a object movement along a path Which i have been geting from Navmesh Unity3d
I am using coroutine in which i controled it with while loop as i can show
public void DrawPath(NavMeshPath pathParameter, GameObject go)
{
Debug.Log("path Parameter" + pathParameter.corners.Length);
if (agent == null || agent.path == null)
{
Debug.Log("Returning");
return;
}
line.material = matToApplyOnLineRenderer;
line.SetWidth(1f, 1f);
line.SetVertexCount(pathParameter.corners.Length);
allPoints = new Vector3[pathParameter.corners.Length];
for (int i = 0; i < pathParameter.corners.Length; i++)
{
allPoints[i] = pathParameter.corners[i];
line.SetPosition(i, pathParameter.corners[i]);
}
StartCoroutine(AnimateArrow(pathParameter));
//StartCoroutine(AnimateArrowHigh(pathParameter));
}
#endregion
#region AnimateArrows
void RunAgain()
{
StartCoroutine(AnimateArrow(Navpath));
}
IEnumerator AnimateArrow(NavMeshPath path)
{
Vector3 start;
Vector3 end;
while (true)
{
if (index > 0)
{
if (index != path.corners.Length - 1)
{
start = allPoints[index];
index += 1;
end = allPoints[index];
StopCoroutine("MoveObject");
StartCoroutine(MoveObject(arrow.transform, start, end, 3.0f));
yield return null;
}
else
{
index = 0;
RunAgain();
}
}
else if (index == 0)
{
start = allPoints[index];
arrow.transform.position = allPoints[index];
index += 1;
end = allPoints[index];
StopCoroutine("MoveObject");
StartCoroutine(MoveObject(arrow.transform, start, end, 3.0f));
yield return null;
}
}
}
IEnumerator MoveObject(Transform arrow, Vector3 startPos, Vector3 endPos, float time)
{
float i = 0.0f;
float rate = 1.0f / time;
journeyLength = Vector3.Distance(startPos, endPos);
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
while (i < 1.0f)
{
// Debug.Log("fracJourney In While" + fracJourney);
arrow.position = Vector3.LerpUnclamped(startPos, endPos, fracJourney);
yield return endPos;
}
Debug.LogError("Outside While");
}
But the problem is i have to move object on a constant speed but my object is gaining speed at every loop as i have to make movement in a loop so it tends to move until user wants to end it by input
guys plz help i dont understand what i am doing wrong in Coroutines that the speed of my objects is rising i wat it to stay constant but somehow its not working that way
thanks
As an alternative, you could utilize Unity's AnimationCurve class to map out all kinds of super smooth animation types easily:
You can define the curves in the inspector, or in code
public AnimationCurve Linear
{
get
{
return new AnimationCurve(new Keyframe(0, 0, 1, 1), new Keyframe(1, 1, 1, 1));
}
}
And you can define useage in a coroutine as such:
Vector2.Lerp (startPos, targetPos, aCurve.Evaluate(percentCompleted));
Where "percentCompleted" is your timer/TotalTimeToComplete.
A full example of lerping can be seen from this function:
IEnumerator CoTween(RectTransform aRect, float aTime, Vector2 aDistance, AnimationCurve aCurve, System.Action aCallback = null)
{
float startTime = Time.time;
Vector2 startPos = aRect.anchoredPosition;
Vector2 targetPos = aRect.anchoredPosition + aDistance;
float percentCompleted = 0;
while(Vector2.Distance(aRect.anchoredPosition,targetPos) > .5f && percentCompleted < 1){
percentCompleted = (Time.time - startTime) / aTime;
aRect.anchoredPosition = Vector2.Lerp (startPos, targetPos, aCurve.Evaluate(percentCompleted));
yield return new WaitForEndOfFrame();
if (aRect == null)
{
DeregisterObject(aRect);
yield break;
}
}
DeregisterObject(aRect);
mCallbacks.Add(aCallback);
yield break;
}
Check out this Tween library for more code examples: https://github.com/James9074/Unity-Juice-UI/blob/master/Juice.cs
while (i < 1.0f) will run forever because i is 0.0f and 0.0f is always < 1.0f and there is no place inside your while loop, where you increement i so that it will >= 1.0f. You need a way to exit that while loop. It should have looked like something below:
while (i < 1.0f){
i++ or i= Time.detaTime..... so that this loop will exist at some point.
}
Also your moving function is bad. The function below should do what you are trying to do:
bool isMoving = false;
IEnumerator MoveObject(Transform arrow, Vector3 startPos, Vector3 endPos, float time = 3)
{
//Make sure there is only one instance of this function running
if (isMoving)
{
yield break; ///exit if this is still running
}
isMoving = true;
float counter = 0;
while (counter < time)
{
counter += Time.deltaTime;
arrow.position = Vector3.Lerp(startPos, endPos, counter / time);
yield return null;
}
isMoving = false;
}
Also, in your AnimateArrow(NavMeshPath path) function, replace these three lines of code:
StopCoroutine("MoveObject");
StartCoroutine(MoveObject(arrow.transform, start, end, 3.0f));
yield return null;
with
yield return StartCoroutine(MoveObject(arrow.transform, start, end, 3.0f));
Doing this will wait the MoveObject function to finish before returning and running again in the while loop. You have to replace these inside if (index != path.corners.Length - 1) and else if (index == 0)
Maybe you could multiply the velocity by, say, 0.95f. This will make it accelerate, then stay at a constant speed, and then when you want it to stop it will gradually decelerate. Increasing the 0.95f will cause it to accelerate/decelerate faster.
Basically I have been given a project to develop a game in unity using c# and the device Leap Motion. The problem I am having is that I have a basic power bar that goes up and down through a range of 0 - 100, when the user releases his fist I want to shoot using the selected power. the problem I am having is I cannot figure out a way to set the bool "shoot" to true on release of the fists.
Controller m_leapController;
public GameObject CannonBarrel;
[Range(0, 100)] public float ballPower = 0f;
public bool increasing = true;
public bool shoot = false;
if (frame.Hands.Count >= 2)
{
Hand leftHand = GetLeftMostHand(frame);
Hand rightHand = GetRightMostHand(frame);
Vector3 handDiff = leftHand.PalmPosition.ToUnityScaled() - rightHand.PalmPosition.ToUnityScaled();
Vector3 newRot = CannonBarrel.transform.localRotation.eulerAngles;
float leftRightSpeed = handDiff.y * 5.0f;
float handPitch = leftHand.Direction.Pitch + rightHand.Direction.Pitch * 0.5f;
newRot.x = 0;
newRot.y = 90;
newRot.z = 70 + -handPitch * 20.0f;
shoot = true;
// if closed fist...
if (frame.Fingers.Count < 3)
{
leftRightSpeed = 0;
shoot = false;
if (increasing == true)
{
ballPower++;
if (ballPower >= 100)
{
increasing = false;
}
}
else if (increasing == false)
{
ballPower--;
if (ballPower <= 0)
{
increasing = true;
}
}
}
else
{
//Move left or right depending on hands height difference.
transform.parent.rigidbody.velocity = transform.parent.right * leftRightSpeed;
//Rotate the barrel
CannonBarrel.transform.localRotation = Quaternion.Slerp(CannonBarrel.transform.localRotation, Quaternion.Euler(newRot), 0.1f);
}
if (shoot == true)
{
Debug.Log("fired");
//add code here to spawn projectile
}
}
I hope that this code makes sense to you. All that is happening is if you're holding your fists above the Leap sensor the ball power will increase till you hit 100, then it will decrease back down to 0 and so on until you release your fists. I need a way to set the "shoot" bool to true once a power has been selected as the way I currently do it I can only set it to true either during the power selection or it gets set to true before you clench your fists.
Thanks in advance.
A somewhat simple way you could do this is by setting a bool to check if the player has clenched his fist at least once, thus setting the power. Let's call it hasClenchedFist:
if (frame.Hands.Count >= 2)
{
Hand leftHand = GetLeftMostHand(frame);
Hand rightHand = GetRightMostHand(frame);
Vector3 handDiff = leftHand.PalmPosition.ToUnityScaled() - rightHand.PalmPosition.ToUnityScaled();
Vector3 newRot = CannonBarrel.transform.localRotation.eulerAngles;
float leftRightSpeed = handDiff.y * 5.0f;
float handPitch = leftHand.Direction.Pitch + rightHand.Direction.Pitch * 0.5f;
newRot.x = 0;
newRot.y = 90;
newRot.z = 70 + -handPitch * 20.0f;
//shoot = true;
// if closed fist...
if (frame.Fingers.Count < 3)
{
leftRightSpeed = 0;
hasClenchedFist = true;
shoot = false;
if (increasing == true)
{
ballPower++;
if (ballPower >= 100)
{
increasing = false;
}
}
else if (increasing == false)
{
ballPower--;
if (ballPower <= 0)
{
increasing = true;
}
}
}
else if(hasClenchedFist)
{
shoot = true;
}
else
{
//Move left or right depending on hands height difference.
transform.parent.rigidbody.velocity = transform.parent.right * leftRightSpeed;
//Rotate the barrel
CannonBarrel.transform.localRotation = Quaternion.Slerp(CannonBarrel.transform.localRotation, Quaternion.Euler(newRot), 0.1f);
}
if (shoot == true)
{
Debug.Log("fired");
//add code here to spawn projectile
}
}
This should work, unless I've misunderstood the code/requirements.