Blog Etienne Faucher - Programmation
mercredi 10 février 2016
Unity 3D with Bitbucket
http://www.gamasutra.com/blogs/BurkayOzdemir/20130303/187697/Using_Unity3D_with_external_VCS_Mercurial__Bitbucket_step_to_step_guide.php
jeudi 15 octobre 2015
Blender créer un personnage
http://cgi.tutsplus.com/tutorials/creating-a-low-poly-ninja-game-character-using-blender-part-1--cg-16132
http://cgi.tutsplus.com/tutorials/creating-a-low-poly-ninja-game-character-using-blender-part-2--cg-16133
http://cgi.tutsplus.com/tutorials/creating-a-low-poly-ninja-game-character-using-blender-part-2--cg-16133
mercredi 23 septembre 2015
Unity ThirdPersonController.js to ThirdPersonController.cs
1: using UnityEngine;
2: using System.Collections;
3: public class ThirdPersonController : MonoBehaviour {
4: public AnimationClip idleAnimation;
5: public AnimationClip walkAnimation;
6: public AnimationClip runAnimation;
7: public AnimationClip jumpPoseAnimation;
8: public float walkMaxAnimationSpeed = 0.75f;
9: public float trotMaxAnimationSpeed= 1.0f;
10: public float runMaxAnimationSpeed = 1.0f;
11: public float jumpAnimationSpeed = 1.15f;
12: public float landAnimationSpeed = 1.0f;
13: private Animation _animation;
14: // The speed when walking
15: public float walkSpeed = 2.0f;
16: // after trotAfterSeconds of walking we trot with trotSpeed
17: public float trotSpeed = 4.0f;
18: // when pressing "Fire3" button (cmd) we start running
19: public float runSpeed = 6.0f;
20: public float inAirControlAcceleration = 3.0f;
21: // How high do we jump when pressing jump and letting go immediately
22: public float jumpHeight = 0.5f;
23: // The gravity for the character
24: public float gravity = 20.0f;
25: // The gravity in controlled descent mode
26: public float speedSmoothing = 10.0f;
27: public float rotateSpeed = 500.0f;
28: public float trotAfterSeconds = 3.0f;
29: public bool canJump = true;
30: private float jumpRepeatTime = 0.05f;
31: private float jumpTimeout = 0.15f;
32: private float groundedTimeout = 0.25f;
33: // The camera doesnt start following the target immediately but waits for a split second to avoid too much waving around.
34: private float lockCameraTimer = 0.0f;
35: // The current move direction in x-z
36: private Vector3 moveDirection = Vector3.zero;
37: // The current vertical speed
38: private float verticalSpeed = 0.0f;
39: // The current x-z move speed
40: private float moveSpeed = 0.0f;
41: // The last collision flags returned from controller.Move
42: private CollisionFlags collisionFlags;
43: // Are we jumping? (Initiated with jump button and not grounded yet)
44: private bool jumping = false;
45: private bool jumpingReachedApex = false;
46: // Are we moving backwards (This locks the camera to not do a 180 degree spin)
47: private bool movingBack = false;
48: // Is the user pressing any keys?
49: private bool isMoving = false;
50: // When did the user start walking (Used for going into trot after a while)
51: private float walkTimeStart = 0.0f;
52: // Last time the jump button was clicked down
53: private float lastJumpButtonTime = -10.0f;
54: // Last time we performed a jump
55: private float lastJumpTime = -1.0f;
56: // the height we jumped from (Used to determine for how long to apply extra jump power after jumping.)
57: private float lastJumpStartHeight = 0.0f;
58: private Vector3 inAirVelocity = Vector3.zero;
59: private float lastGroundedTime = 0.0f;
60: private bool isControllable = true;
61: enum CharacterState {
62: Idle = 0,
63: Walking = 1,
64: Trotting = 2,
65: Running = 3,
66: Jumping = 4,
67: }
68: private CharacterState _characterState;
69: // Use this for initialization
70: void Start () {
71: }
72: void Awake () {
73: moveDirection = transform.TransformDirection(Vector3.forward);
74: _animation = GetComponent <Animation> ();
75: if(!_animation)
76: Debug.Log("The character you would like to control doesn't have animations. Moving her might look weird.");
77: /*
78: public var idleAnimation : AnimationClip;
79: public var walkAnimation : AnimationClip;
80: public var runAnimation : AnimationClip;
81: public var jumpPoseAnimation : AnimationClip;
82: */
83: if(!idleAnimation) {
84: _animation = null;
85: Debug.Log("No idle animation found. Turning off animations.");
86: }
87: if(!walkAnimation) {
88: _animation = null;
89: Debug.Log("No walk animation found. Turning off animations.");
90: }
91: if(!runAnimation) {
92: _animation = null;
93: Debug.Log("No run animation found. Turning off animations.");
94: }
95: if(!jumpPoseAnimation && canJump) {
96: _animation = null;
97: Debug.Log("No jump animation found and the character has canJump enabled. Turning off animations.");
98: }
99: }
100: void UpdateSmoothedMovementDirection ()
101: {
102: Transform cameraTransform = Camera.main.transform;
103: bool grounded = IsGrounded();
104: // Forward vector relative to the camera along the x-z plane
105: Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
106: forward.y = 0;
107: forward = forward.normalized;
108: // Right vector relative to the camera
109: // Always orthogonal to the forward vector
110: Vector3 right = new Vector3(forward.z, 0, -forward.x);
111: float v = Input.GetAxisRaw("Vertical");
112: float h = Input.GetAxisRaw("Horizontal");
113: // Are we moving backwards or looking backwards
114: if (v < -0.2f)
115: movingBack = true;
116: else
117: movingBack = false;
118: bool wasMoving = isMoving;
119: isMoving = Mathf.Abs (h) > 0.1f || Mathf.Abs (v) > 0.1f;
120: // Target direction relative to the camera
121: Vector3 targetDirection = h * right + v * forward;
122: // Grounded controls
123: if (grounded)
124: {
125: // Lock camera for short period when transitioning moving & standing still
126: lockCameraTimer += Time.deltaTime;
127: if (isMoving != wasMoving)
128: lockCameraTimer = 0.0f;
129: // We store speed and direction seperately,
130: // so that when the character stands still we still have a valid forward direction
131: // moveDirection is always normalized, and we only update it if there is user input.
132: if (targetDirection != Vector3.zero)
133: {
134: // If we are really slow, just snap to the target direction
135: if (moveSpeed < walkSpeed * 0.9f && grounded)
136: {
137: moveDirection = targetDirection.normalized;
138: }
139: // Otherwise smoothly turn towards it
140: else
141: {
142: moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
143: moveDirection = moveDirection.normalized;
144: }
145: }
146: // Smooth the speed based on the current target direction
147: float curSmooth = speedSmoothing * Time.deltaTime;
148: // Choose target speed
149: //* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
150: float targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0f);
151: _characterState = CharacterState.Idle;
152: // Pick speed modifier
153: if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift))
154: {
155: targetSpeed *= runSpeed;
156: _characterState = CharacterState.Running;
157: }
158: else if (Time.time - trotAfterSeconds > walkTimeStart)
159: {
160: targetSpeed *= trotSpeed;
161: _characterState = CharacterState.Trotting;
162: }
163: else
164: {
165: targetSpeed *= walkSpeed;
166: _characterState = CharacterState.Walking;
167: }
168: moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
169: // Reset walk time start when we slow down
170: if (moveSpeed < walkSpeed * 0.3f)
171: walkTimeStart = Time.time;
172: }
173: // In air controls
174: else
175: {
176: // Lock camera while in air
177: if (jumping)
178: lockCameraTimer = 0.0f;
179: if (isMoving)
180: inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
181: }
182: }
183: void ApplyJumping ()
184: {
185: // Prevent jumping too fast after each other
186: if (lastJumpTime + jumpRepeatTime > Time.time)
187: return;
188: if (IsGrounded()) {
189: // Jump
190: // - Only when pressing the button down
191: // - With a timeout so you can press the button slightly before landing
192: if (canJump && Time.time < lastJumpButtonTime + jumpTimeout) {
193: verticalSpeed = CalculateJumpVerticalSpeed (jumpHeight);
194: SendMessage("DidJump", SendMessageOptions.DontRequireReceiver);
195: }
196: }
197: }
198: void ApplyGravity ()
199: {
200: if (isControllable) // don't move player at all if not controllable.
201: {
202: // Apply gravity
203: bool jumpButton = Input.GetButton("Jump");
204: // When we reach the apex of the jump we send out a message
205: if (jumping && !jumpingReachedApex && verticalSpeed <= 0.0f)
206: {
207: jumpingReachedApex = true;
208: SendMessage("DidJumpReachApex", SendMessageOptions.DontRequireReceiver);
209: }
210: if (IsGrounded ())
211: verticalSpeed = 0.0f;
212: else
213: verticalSpeed -= gravity * Time.deltaTime;
214: }
215: }
216: float CalculateJumpVerticalSpeed (float targetJumpHeight)
217: {
218: // From the jump height and gravity we deduce the upwards speed
219: // for the character to reach at the apex.
220: return Mathf.Sqrt(2 * targetJumpHeight * gravity);
221: }
222: void DidJump ()
223: {
224: jumping = true;
225: jumpingReachedApex = false;
226: lastJumpTime = Time.time;
227: lastJumpStartHeight = transform.position.y;
228: lastJumpButtonTime = -10;
229: _characterState = CharacterState.Jumping;
230: }
231: void OnControllerColliderHit (ControllerColliderHit hit)
232: {
233: // Debug.DrawRay(hit.point, hit.normal);
234: if (hit.moveDirection.y > 0.01f)
235: return;
236: }
237: float GetSpeed () {
238: return moveSpeed;
239: }
240: public bool IsJumping () {
241: return jumping;
242: }
243: bool IsGrounded () {
244: return (collisionFlags & CollisionFlags.CollidedBelow) != 0;
245: }
246: Vector3 GetDirection () {
247: return moveDirection;
248: }
249: public bool IsMovingBackwards () {
250: return movingBack;
251: }
252: public float GetLockCameraTimer ()
253: {
254: return lockCameraTimer;
255: }
256: bool IsMoving ()
257: {
258: return Mathf.Abs(Input.GetAxisRaw("Vertical")) + Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5f;
259: }
260: bool HasJumpReachedApex ()
261: {
262: return jumpingReachedApex;
263: }
264: bool IsGroundedWithTimeout ()
265: {
266: return lastGroundedTime + groundedTimeout > Time.time;
267: }
268: void Reset ()
269: {
270: gameObject.tag = "Player";
271: }
272: // Update is called once per frame
273: void Update () {
274: if (!isControllable)
275: {
276: // kill all inputs if not controllable.
277: Input.ResetInputAxes();
278: }
279: if (Input.GetButtonDown ("Jump"))
280: {
281: lastJumpButtonTime = Time.time;
282: }
283: UpdateSmoothedMovementDirection();
284: // Apply gravity
285: // - extra power jump modifies gravity
286: // - controlledDescent mode modifies gravity
287: ApplyGravity ();
288: // Apply jumping logic
289: ApplyJumping ();
290: // Calculate actual motion
291: Vector3 movement = moveDirection * moveSpeed + new Vector3 (0, verticalSpeed, 0) + inAirVelocity;
292: movement *= Time.deltaTime;
293: // Move the controller
294: CharacterController controller = GetComponent <CharacterController> ();
295: collisionFlags = controller.Move(movement);
296: // ANIMATION sector
297: if(_animation) {
298: if(_characterState == CharacterState.Jumping)
299: {
300: if(!jumpingReachedApex) {
301: _animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
302: _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
303: _animation.CrossFade(jumpPoseAnimation.name);
304: } else {
305: _animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
306: _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
307: _animation.CrossFade(jumpPoseAnimation.name);
308: }
309: }
310: else
311: {
312: if(controller.velocity.sqrMagnitude < 0.1) {
313: _animation.CrossFade(idleAnimation.name);
314: }
315: else
316: {
317: if(_characterState == CharacterState.Running) {
318: _animation[runAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0f, runMaxAnimationSpeed);
319: _animation.CrossFade(runAnimation.name);
320: }
321: else if(_characterState == CharacterState.Trotting) {
322: _animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0f, trotMaxAnimationSpeed);
323: _animation.CrossFade(walkAnimation.name);
324: }
325: else if(_characterState == CharacterState.Walking) {
326: _animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0f, walkMaxAnimationSpeed);
327: _animation.CrossFade(walkAnimation.name);
328: }
329: }
330: }
331: }
332: // ANIMATION sector
333: // Set rotation to the move direction
334: if (IsGrounded())
335: {
336: transform.rotation = Quaternion.LookRotation(moveDirection);
337: }
338: else
339: {
340: var xzMove = movement;
341: xzMove.y = 0;
342: if (xzMove.sqrMagnitude > 0.001f)
343: {
344: transform.rotation = Quaternion.LookRotation(xzMove);
345: }
346: }
347: // We are in jump mode but just became grounded
348: if (IsGrounded())
349: {
350: lastGroundedTime = Time.time;
351: inAirVelocity = Vector3.zero;
352: if (jumping)
353: {
354: jumping = false;
355: SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
356: }
357: }
358: }
359: }
Unity ThirdPersonCamera.js to ThirdPersonCamera.cs
1: using UnityEngine;
2: using System.Collections;
3: public class ThirdPersonCamera : MonoBehaviour {
4: Transform cameraTransform;
5: private Transform _target;
6: // The distance in the x-z plane to the target
7: public float distance = 7.0f;
8: // the height we want the camera to be above the target
9: public float height = 3.0f;
10: public float angularSmoothLag = 0.3f;
11: public float angularMaxSpeed = 15.0f;
12: public float heightSmoothLag = 0.3f;
13: public float snapSmoothLag = 0.2f;
14: public float snapMaxSpeed = 720.0f;
15: public float clampHeadPositionScreenSpace = 0.75f;
16: public float lockCameraTimeout = 0.2f;
17: private Vector3 headOffset = Vector3.zero;
18: private Vector3 centerOffset = Vector3.zero;
19: private float heightVelocity = 0.0f;
20: private float angleVelocity = 0.0f;
21: private bool snap = false;
22: private ThirdPersonController controller;
23: private float targetHeight = 100000.0f;
24: void Awake ()
25: {
26: if(!cameraTransform && Camera.main)
27: cameraTransform = Camera.main.transform;
28: if(!cameraTransform) {
29: Debug.Log("Please assign a camera to the ThirdPersonCamera script.");
30: enabled = false;
31: }
32: _target = transform;
33: if (_target)
34: {
35: controller = _target.GetComponent <ThirdPersonController> ();
36: }
37: if (controller)
38: {
39: Collider characterController = _target.GetComponent <Collider> ();
40: centerOffset = characterController.bounds.center - _target.position;
41: headOffset = centerOffset;
42: headOffset.y = characterController.bounds.max.y - _target.position.y;
43: }
44: else
45: Debug.Log("Please assign a target to the camera that has a ThirdPersonController script attached.");
46: Cut(_target, centerOffset);
47: }
48: float AngleDistance (float a, float b)
49: {
50: a = Mathf.Repeat(a, 360);
51: b = Mathf.Repeat(b, 360);
52: return Mathf.Abs(b - a);
53: }
54: void DebugDrawStuff ()
55: {
56: Debug.DrawLine(_target.position, _target.position + headOffset);
57: }
58: void Apply (Transform dummyTarget , Vector3 dummyCenter)
59: {
60: // Early out if we don't have a target
61: if (!controller)
62: return;
63: Vector3 targetCenter = _target.position + centerOffset;
64: Vector3 targetHead = _target.position + headOffset;
65: // DebugDrawStuff();
66: // Calculate the current & target rotation angles
67: float originalTargetAngle = _target.eulerAngles.y;
68: float currentAngle = cameraTransform.eulerAngles.y;
69: // Adjust real target angle when camera is locked
70: float targetAngle = originalTargetAngle;
71: // When pressing Fire2 (alt) the camera will snap to the target direction real quick.
72: // It will stop snapping when it reaches the target
73: if (Input.GetButton("Fire2"))
74: snap = true;
75: if (snap)
76: {
77: // We are close to the target, so we can stop snapping now!
78: if (AngleDistance (currentAngle, originalTargetAngle) < 3.0)
79: snap = false;
80: currentAngle = Mathf.SmoothDampAngle(currentAngle, targetAngle, ref angleVelocity, snapSmoothLag, snapMaxSpeed);
81: }
82: // Normal camera motion
83: else
84: {
85: if (controller.GetLockCameraTimer () < lockCameraTimeout)
86: {
87: targetAngle = currentAngle;
88: }
89: // Lock the camera when moving backwards!
90: // * It is really confusing to do 180 degree spins when turning around.
91: if (AngleDistance (currentAngle, targetAngle) > 160 && controller.IsMovingBackwards ())
92: targetAngle += 180;
93: currentAngle = Mathf.SmoothDampAngle(currentAngle, targetAngle, ref angleVelocity, angularSmoothLag, angularMaxSpeed);
94: }
95: // When jumping don't move camera upwards but only down!
96: if (controller.IsJumping ())
97: {
98: // We'd be moving the camera upwards, do that only if it's really high
99: float newTargetHeight = targetCenter.y + height;
100: if (newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5)
101: targetHeight = targetCenter.y + height;
102: }
103: // When walking always update the target height
104: else
105: {
106: targetHeight = targetCenter.y + height;
107: }
108: // Damp the height
109: float currentHeight = cameraTransform.position.y;
110: currentHeight = Mathf.SmoothDamp (currentHeight, targetHeight, ref heightVelocity, heightSmoothLag);
111: // Convert the angle into a rotation, by which we then reposition the camera
112: Quaternion currentRotation = Quaternion.Euler (0, currentAngle, 0);
113: // Set the position of the camera on the x-z plane to:
114: // distance meters behind the target
115: cameraTransform.position = targetCenter;
116: cameraTransform.position += currentRotation * Vector3.back * distance;
117: // Set the height of the camera
118: cameraTransform.position = new Vector3 (cameraTransform.position.x,currentHeight,cameraTransform.position.z);
119: // Always look at the target
120: SetUpRotation(targetCenter, targetHead);
121: }
122: void Cut (Transform dummyTarget, Vector3 dummyCenter)
123: {
124: float oldHeightSmooth = heightSmoothLag;
125: float oldSnapMaxSpeed = snapMaxSpeed;
126: float oldSnapSmooth = snapSmoothLag;
127: snapMaxSpeed = 10000;
128: snapSmoothLag = 0.001f;
129: heightSmoothLag = 0.001f;
130: snap = true;
131: Apply (transform, Vector3.zero);
132: heightSmoothLag = oldHeightSmooth;
133: snapMaxSpeed = oldSnapMaxSpeed;
134: snapSmoothLag = oldSnapSmooth;
135: }
136: void SetUpRotation (Vector3 centerPos, Vector3 headPos)
137: {
138: // Now it's getting hairy. The devil is in the details here, the big issue is jumping of course.
139: // * When jumping up and down we don't want to center the guy in screen space.
140: // This is important to give a feel for how high you jump and avoiding large camera movements.
141: //
142: // * At the same time we dont want him to ever go out of screen and we want all rotations to be totally smooth.
143: //
144: // So here is what we will do:
145: //
146: // 1. We first find the rotation around the y axis. Thus he is always centered on the y-axis
147: // 2. When grounded we make him be centered
148: // 3. When jumping we keep the camera rotation but rotate the camera to get him back into view if his head is above some threshold
149: // 4. When landing we smoothly interpolate towards centering him on screen
150: Vector3 cameraPos = cameraTransform.position;
151: Vector3 offsetToCenter = centerPos - cameraPos;
152: // Generate base rotation only around y-axis
153: Quaternion yRotation = Quaternion.LookRotation(new Vector3(offsetToCenter.x, 0, offsetToCenter.z));
154: Vector3 relativeOffset = Vector3.forward * distance + Vector3.down * height;
155: cameraTransform.rotation = yRotation * Quaternion.LookRotation(relativeOffset);
156: // Calculate the projected center position and top position in world space
157: Ray centerRay = cameraTransform.GetComponent <Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 1));
158: Ray topRay = cameraTransform.GetComponent <Camera>().ViewportPointToRay(new Vector3(0.5f, clampHeadPositionScreenSpace, 1));
159: Vector3 centerRayPos = centerRay.GetPoint(distance);
160: Vector3 topRayPos = topRay.GetPoint(distance);
161: float centerToTopAngle = Vector3.Angle(centerRay.direction, topRay.direction);
162: float heightToAngle = centerToTopAngle / (centerRayPos.y - topRayPos.y);
163: float extraLookAngle = heightToAngle * (centerRayPos.y - centerPos.y);
164: if (extraLookAngle < centerToTopAngle)
165: {
166: extraLookAngle = 0;
167: }
168: else
169: {
170: extraLookAngle = extraLookAngle - centerToTopAngle;
171: cameraTransform.rotation *= Quaternion.Euler(-extraLookAngle, 0, 0);
172: }
173: }
174: Vector3 GetCenterOffset ()
175: {
176: return centerOffset;
177: }
178: // Use this for initialization
179: void Start () {
180: }
181: // Update is called once per frame
182: void Update () {
183: }
184: void LateUpdate () {
185: Apply (transform, Vector3.zero);
186: }
187: }
Format code for blog
http://danielthat.blogspot.ca/2013/03/how-to-display-code-in-blogger-posts.html
http://codeformatter.blogspot.in/2009/06/about-code-formatter.html
http://codeformatter.blogspot.in/2009/06/about-code-formatter.html
mercredi 2 septembre 2015
Unity 3D création d'un arc avec Line Renderer/ Unity 3D create Arc with Line Renderer
J'ai téléchargé le projet fait par l'usager UVRadiation sur le forum;
http://forum.unity3d.com/threads/projection-trajectory-arc.12588/
Le code était en Javascript, je l'ai donc converti en C# et fait la modification pour que l'arc soit créé à partir de 3 point (début, milieu et fin). Le mouvement de la souris modifie la position de l'objet fin ce qui agrandit et diminue l'arc.
J'ai aussi pris les fonctions du script Bezier que j'ai inclus directement dans le script ArcBehavior.cs
Eng/
I downloaded the project done by UVRadiation user forum ;
http://forum.unity3d.com/threads/projection-trajectory-arc.12588/
The code was in Javascript, so I converted to C # and made the change to the arc is created from three points (start, middle and end) . The movement of the mouse changes the position of the end object which enlarges and reduces the arc.
I also took the functions of Bezier script I included directly in the script ArcBehavior.cs
http://forum.unity3d.com/threads/projection-trajectory-arc.12588/
Le code était en Javascript, je l'ai donc converti en C# et fait la modification pour que l'arc soit créé à partir de 3 point (début, milieu et fin). Le mouvement de la souris modifie la position de l'objet fin ce qui agrandit et diminue l'arc.
J'ai aussi pris les fonctions du script Bezier que j'ai inclus directement dans le script ArcBehavior.cs
Eng/
I downloaded the project done by UVRadiation user forum ;
http://forum.unity3d.com/threads/projection-trajectory-arc.12588/
The code was in Javascript, so I converted to C # and made the change to the arc is created from three points (start, middle and end) . The movement of the mouse changes the position of the end object which enlarges and reduces the arc.
I also took the functions of Bezier script I included directly in the script ArcBehavior.cs
1: using UnityEngine;
2: using System.Collections;
3: public class ArcBehaviourC1: MonoBehaviour {
4: public Transform start;
5: public Transform middle;
6: public Transform end;
7: public float maxHeight = 200;
8: public float maxWidth = 200 ;
9: public int sections = 10;
10: int floorMask;
11: Ray shootRay;
12: LineRenderer GrenadeLine;
13: float camRayLength = 50f;
14: bool raycastok;
15: // Use this for initialization
16: void Start () {
17: GrenadeLine = GetComponent <LineRenderer> ();
18: floorMask = LayerMask.GetMask ("Floor");
19: GrenadeLine.SetVertexCount(sections);
20: }
21: // Update is called once per frame
22: void Update () {
23: Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
24: RaycastHit floorHit;
25: shootRay.origin = transform.position;
26: shootRay.direction = transform.forward;
27: raycastok = Physics.Raycast (camRay, out floorHit, camRayLength, floorMask);
28: Vector3 objectToMouse = floorHit.point - transform.position;
29: objectToMouse.y = 0f;
30: if (Mathf.Abs(objectToMouse.z) < 20 && Mathf.Abs(objectToMouse.x) < 20 )
31: {
32: if (raycastok)
33: {
34: GrenadeLine.enabled = true;
35: end.transform.position = floorHit.point;
36: Debug.DrawLine (start.position, middle.position, Color.red);
37: Debug.DrawLine (middle.position, end.position, Color.red);
38: Debug.DrawLine (start.position, end.position, Color.red);
39: Plot (start.position, middle.position, end.position);
40: }
41: else
42: {
43: GrenadeLine.enabled = false;
44: }
45: }
46: else
47: {
48: GrenadeLine.enabled = false;
49: }
50: }
51: Vector3 GetQuadraticCoordinates(float t , Vector3 p0, Vector3 c0, Vector3 p1)
52: {
53: Vector3 Result;
54: Result = Mathf.Pow(1-t,2)*p0 + 2*t*(1-t)*c0 + Mathf.Pow(t,2)*p1 ;
55: return Result;
56: }
57: void Plot(Vector3 p0 , Vector3 c0,Vector3 p1 )
58: {
59: float t;
60: for (int i = 0 ; i < sections ; i++ )
61: {
62: t = (float)i/(sections-1);
63: GrenadeLine.SetPosition (i ,GetQuadraticCoordinates(t , p0 , c0 , p1 ));
64: }
65: }
66: }
vendredi 17 juillet 2015
Unity 3D RTS Tutoriel
http://stormtek.geek.nz/rts_tutorial/part6.php
S'abonner à :
Messages (Atom)